1
votes

I have a integration test but the problem is, that it uses the datasources from the main application.properties which is a mssql database. In my tests I want to use a h2 database for that I have created a application-test.poperties in src/test/resources . In my test class I defined the @TestPropertySource which links to this property file. But in the log output I can see that the testclass still uses the mssql database connection.

Here is my test class

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @Transactional
    @TestPropertySource(locations="classpath:application-test.properties")
    public class UserControllerTest {

        @LocalServerPort
        private int port;
        TestRestTemplate restTemplate = new TestRestTemplate();
        HttpHeaders headers = new HttpHeaders();
 ...

Here is my src/test/resources/application-test.properties file

    spring.datasource.datasource.url=jdbc:h2:mem:scserver
    spring.datasource.username=sa
    spring.datasource.password=
    spring.datasource.driverClassName=org.h2.Driver
    spring.datasource.jpa.database-platform=org.hibernate.dialect.H2Dialect
   spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect

    # Hibernate ddl auto (create, create-drop, validate, update)
    spring.jpa.hibernate.ddl-auto=create-drop


    #logging
    logging.level.root=info
    logging.file=foo-spring-rest.log

    #required for SpringBootTest does not know why
    spring.main.allow-bean-definition-overriding=true
    spring.h2.console.enabled=true
    spring.h2.console.path=/h2-console
1
Try naming it application.properties instead of application-test.properties. If it's in src/test/resources it should automatically override the one in src/main/resourcesMatt

1 Answers

2
votes

try to use instead of

@TestPropertySource(locations="classpath:application-test.properties")

this

@TestPropertySource(locations = {"classpath:application-test.properties"})
// dont forget the curvy brackets, because location'S' 

or the profile annotation

@ActiveProfiles("test")