Search code examples
javaspringspring-bootspring-data-jpaspring-boot-test

SpringBoot test configuration


I've started to learn Spring Boot in version 2.1.0 and I made a simple application Controller->Service->Repository->Database(H2). Very, very simple application just to start with Spring Boot.

I've read that in that framework I can add under src/resources a file data.sql where I can define insert/update etc. and after run my application that data will be stored. Everything works ok, but when I wanted to write test for my service, just to check if my repository works ok (I test DB for learning purpose) I see that while I run my test in my DB there are already values from data.sql, but I don't have data.sql under test folder. It is under src.

Maybe someone knows, how to configure project that not to take this data.sql from src/resources? Is it possible? Maybe I have to add some more annotations?

**** EDIT ****

This is my repo:


Solution

  • You can create test application-test.properties in test/resources and override in in test like this:

    @RunWith(SpringRunner.class)
    @SpringApplicationConfiguration(classes = ExampleApplication.class)
    @TestPropertySource(locations="classpath:test.properties")
    public class ExampleApplicationTests {
    
    }
    

    Then in you new created application-test.properties file block running your script:

    spring.datasource.initialization-mode=never