In spring boot I'm trying to create my first transactional test, but the trasaction doesn't work.
@TestPropertySource(locations = "classpath:test.properties")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringJUnit4ClassRunner.class)
//@RunWith(SpringRunner.class)
@Transactional
@TestExecutionListeners(
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS,
listeners = {TransactionalTestExecutionListener.class}
)
public class IntegrationTests
{
@Autowired
TemperatureLogRepository temperatureLogRepository;
@Test
@SqlGroup({
@Sql(
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
config = @SqlConfig(transactionMode = ISOLATED),
scripts = "classpath:sqls/insertRecords2.sql"
)
})
public void firstRepoTest() throws SQLException
{
assertThat(temperatureLogRepository.getFullList().size()).isEqualByComparingTo(0);
}
}
I know that SqlGroup is not necessary, but there will be more files added.
That I have now:
- SQL file executed well and inserted to the DB.
- The
getFullList()
method can read it and returns with the right data. - After the test I still have the data in the DB, there is no rollback on the transaction.
I'm not exactly sure they are running in the same transaction. Is it possible to be the data commited to the db before the getFullList()
method run?
What I need:
@Sql
inserts data to the transaction.getFullList()
read the data from the transaction.- Test the returned data.
- Rollback the transaction.