Andrey Yaskulsky
Andrey Yaskulsky

Reputation: 2516

spring boot test loads schema.sql defined in java/resources instead of test/resources

I have the following Test:

@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest(classes = {AccountsServerTest.class, PostgresSQLTestDbConfig.class})
@ActiveProfiles("test")
public class NodeRepositoryTest {
......
}

and also I have schema.sql and data.sql scripts under src/main/resources which are supposed to be run when the app starts.

I also have another two sql files under src/test/resources/ and I want to run them when NodeRepositoryTest is started.

However, for some reason, when I start NodeRepositoryTest, scripts from src/main/resources are executed.

Perhaps, someone had the same problem before?

I would really appreciate any help,

Thanks

Upvotes: 4

Views: 6219

Answers (1)

rieckpil
rieckpil

Reputation: 12021

You can manually define the .sql scripts which should be applied to your test method/test class.

Have a look at the following example from the official documentation (https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html):

@Test
@Sql({"/test-schema.sql", "/test-user-data.sql"})
public void userTest {
    // execute code that relies on the test data
}

Upvotes: 3

Related Questions