0

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:

Kacu
  • 438
  • 4
  • 23
  • You sure that db exists outside of your app and when you run your test it just takes data from database? Or it is in-memory db? If you dont want to use data from db you should mock your repository – miljon Nov 04 '18 at 19:58
  • I use in-memory DB. Why when I run my ServiceTest SpringBoot scan my app structure and even when ServiceTest is under test//ServiceTest it takes data.sql which is under src/resources? – Kacu Nov 04 '18 at 20:26
  • And I'm sure that it takes data from my in-memory DB, because in data.sql I have 2 inserts and in my test when I test for example "getAll()" I expect 0 results but I got 2. – Kacu Nov 04 '18 at 20:28
  • by the way how you use repository in test? – miljon Nov 04 '18 at 21:04
  • I link my repo, you can check. – Kacu Nov 04 '18 at 23:23

1 Answers1

1

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
Sanket Patel
  • 227
  • 1
  • 14
miljon
  • 2,611
  • 1
  • 16
  • 19