If you wish to only influence a single test with the script, just add on the method.
For example
@Sql({ "classpath:insertMessage.sql" })
@Test
public void myExtremlyBadTestMethodNamingConventionTest() {
If you wish to execute a specified test before all your tests for a single test class. This is where it gets tricky -
First off you might consider adding the @SQL to a @BeforeClass, but
this is not possible since the @SQL does not support the @BeforeClass
(or @Before).
Next, you might discover the @Commit
annotation, which means you can
commit all the changes made with a Test. (@Transaction
's default
behavior is to roll back all changes after the test) Now, you might
consider, a @Commit
single dummy test to execute before all your
other tests?
NO Please do not go down this route.
JUnit does not guarantee the order of the execution of
tests. (This is by design) Because your tests should not be relying on
the result of other tests.
They should be independent of each other. Why? One advantage is,
tests can then be executed in parallel. But also, you would never
want a new test to break exciting tests, it is just a maintenance
nightmare waiting to happen. (#truestory)
Additionally, In the case, you wish to execute a specified script before all your test, you can add an import.sql file to your test's resources. Spring Boot will automatically check for it before your tests and execute it for you. In other words, it will set up your database in a specific state for your all tests.