Initialising a database before Spring Boot test

There are various options if you do not want to execute that explicitly from @Before JUnit hook.

  1. Use Spring Boot's JDBC initialization feature, where you would place schema.sql or data.sql into src/test/resources folder, so that it would be picked up only during testing.
  2. Use Spring's @Sql annotation

You can use @Sql annotaion for populate your DB, for example^

@Sql(scripts = "classpath:db/populateDB.sql")

The above answers all use the .sql schema loading technique where I'd have to have a .sql schema for tests. I didn't want to do it that way as my schema would be expanding and I'd rather not go through the hassle of adding entries to the schema as my tests expand.

As I'm using Spring Boot, I came across this annotation which seems to solve the issue by first running bootRun and then running the tests.

In my test annotations I replaced the @ContextConfigurations with @SpringApplicationConfiguration and left all the classes to be the same. This seemed to solve the issue. So now the test task invokes bootRun to load the classes and then runs the tests.

See @SpringApplicationConfiguration

Hop this helps anyone facing the same issue.