Spring Boot Controller IT test not finding class path resources - spring

I have a spring boot web app with both unit tests and IT tests. The application also has a DAL, and I have unit tests in the DAL layer that use an in memory H2 database for tests. The h2 database is created using the same liquibase changelog as my production environment.
The changelogs are packaged in a jar and referenced in my DAL test by adding this to my application.yml file:
liquibase:
change-log: classpath:src/changeLogs/db-changelog-master.xml
This works fine in my DAL project but not in my web project. In my web project I get this error:
Cannot find changelog location: class path resource [src/changeLogs/db-changelog-master.xml]
The test that fails to find the changelog is a Spring Boot IT test. The class under test is a Spring Controller. The test is annotated like this:
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles({"IT","development"})
Any ideas what is happening here? Thanks.

Related

Spring integration test has different behaviour when started via IntelliJ run configuration and via mvn verify

I am working on the Spring application based on Spring Boot 2.6.7 (Spring framework version 5.3.19). I noticed that when I run the integration test via IntelliJ run configuration (basically created only by the right clicking on the test method name and choosing Run test) then the same instance of the ApplicationContext is used in the integration test class and in the actual SpringBootApplication which is tested by the integration test.
But when the same integration test is executed from the command line via mvn verify command, then the different instance of the ApplicationContext is active in the Spring Boot Application from the one which is active in the integration test class.
That for example has a consequence that spring data repository which I added as a field to the integration test class with #SpyBean annotation is not not applied in the Spring boot application. In the applicationContext of the integration test class, that spring data repository is registered as a spy but in the application context of the Spring Boot Application there is no spy proxy but the regular repository.
On the other hand when the test is running via IntelliJ run configuration, the applicationContext is same everywhere and the spy bean is active in the Spring Boot Application flow.
So I want to achieve the same behavior when I run mvn verify as I achieve when I run the test from IntelliJ. Any ideas?

Springboot application getting Conflicting persistence unit definition for name default

In my spring boot application, I am using some jar files which have inner jars.Those inner jars have separate persistence.xml file and using default as persistence unit name. Now while I am trying to run my spring boot application, I am getting conflicting persistence unit definition for name default. Does somebody know how to resolve this?

running integration tests in teamcity using spring-boot and restassured

I am writing my integration tests using springboot and rest-assured and using SpringApplicationConfiguration to load the configuration.
This is what the test class annotation look like:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = RestAPIApplication.class)
#IntegrationTest("server.port:8083") // this is the port set by my application
Now comes my real question. When I run the test class in teamcity using maven, don't I have to configure something to run the springboot server before running the integration tests?
I am new to springboot so maybe this question is very novice but please point me to the correct path.
If you're using embedded tomcat, no extra setup is needed-- simply run the maven tests.

Testing with spring configuration and Mocking objects

I am using a spring configuration in testing like below:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:config/spring/config.xml" })
Let's say I'm testing a service that depends on other services and a repository. Using the #ContextConfiguration these beans will be created and autowired into the service I am testing.
Is it considered better to mock these dependencies and repositories? When creating and loading a test configuration as done currently, a problem in a dependency could cause the test to fail, so I am not really only testing that one service but the other dependencies as well.
Should these dependencies be mocked in the unit test (since you are only supposed to test the functionality of that class and not its dependencies) but instantiated and wired in during integration tests?

FileNotFoundException in a testing a Maven module

I'm working on a recently mavenized legacy project with following multi-modular structure:
Parent:
Web
Service
Dao
("Service" module is dependent on "Dao" module)
Problem: some tests of Service classes call DAO code that creates beans using Spring's ClassPathXmlApplicationContext (this part is not really DAOs but caching related). Since, ClassPathXmlApplicationContext uses spring config xml of the DAO module - the Service tests fail throwing FileNotFoundException. I think this is because tests run in Service module and the spring config xml being referred lies in Dao module.
Please advise on how can I resolve the above issue in tests referring to code/resources of other modules?
Put a copy of the Spring configuration under src/test/resources in the Service module. Quite often you want a different configuration for testing anyway, but also it means your tests are less dependent on configuration changes in another module.

Resources