im using Junit with spring-test and i would like to have a classic transactionnal test with this annotation:
@Injectdata("classpath:src/test/mydata.sql")
@Test
public void myTest throws Exception {
// ...
}
This data will be injected with the jdbcspring template in the same transaction & those datas will be available for
only this test.
Actually, im injecting data this way :
@Test
public void myTest throws Exception {
jdbcTemplate.update("my sql query);
}
I know that Unitils framework do the samething but with a dataset dbunit file.
I have found the solution by creating one myself.
First create the listener used by Spring test:
Than on your class test add:
The TRICK was to add ALL listeners normally added automatically by Spring if you dont add listeners. Avoiding that lead to weird errors.
This is not documented but ive found that without listener with a transactionnal spring test those 3 listeners are added automatically by Spring (thanks the debug mode!)
And finally you cand us this cool annotation like this :
You can even use relative paths or absolute path.
And naturally the insertion will be like others inserts automatically rollback at the end.