0

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.

jokarl
  • 1,913
  • 2
  • 24
  • 52
  • 1
    The trivial part you are missing is that `@PropertySource` works only with property files NOT with yaml files. – M. Deinum Jun 19 '18 at 08:33
  • 1
    @M.Deinum you are a lifesaver. I changed format to `.properties` and it works flawlessly. Thank you! Care to post an answer so I can mark it? – jokarl Jun 19 '18 at 08:43
  • I marked it a duplicate of another question with the same issue. – M. Deinum Jun 19 '18 at 11:04

0 Answers0