I have a test case where I start my entire application context, and I want to swap out the database to an in memory database and disable Flyway.
My main application configuration is located in the root of the project in a folder named config
. My test configuration is located in src/test/resources
. I've tried naming the test resource file both application.yml
and application-test.yml
.
My test looks as follows:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations="classpath:application-test.yml")
public class SmokeTest {
@LocalServerPort
private int port;
@Value("${api.path}")
private String path;
@Value("${spring.datasource.url}")
private String url;
@Autowired
private TestRestTemplate restTemplate;
@Before
public void test() {
System.out.println(url);
}
}
Whatever I do, the url to the datasource is always the one from the main application configuration in the config
folder. My main config has this in it:
flyway:
enabled: true
spring:
datasource:
url: jdbc:postgresql://localhost:5432/datasource
username: user
password: password
And this is the test configration:
flyway:
enabled: false
spring:
datasource:
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password: sa
This is driving me nuts, I feel like I'm missing something trivial.