I have some junit tests where I want to pre-populate the DB with some data that actually makes sense for the test:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class MyTest {
@Sql("test_insert.sql")
@Test
public void testInsert(){
...performs some inserts...
}
@Sql("test_delete.sql") //makes inserts to the db so to delete them
@Test
public void testDelete(){
...
}
I noticed that junit executes the tests by reverse order, meaning that my testDelete will be executed first.
Test insert seems to fail for a "duplicate row constraint" and that happens because the test_delete.sql scripts actually adds that row.
Would it to be possible to rollback the operations made by the @Sql and the test itself so that one test does not influence the others?
@Transactional
. I recommend you read the Testing chapter of the Spring Reference Manual. This is all documented.