My database has triggers. They trigger after a creation of an entity. I want to test a condition, which depends on results of the trigger execution. So, I need to be able to see those results inside my test. But triggers will trigger after a commit, when the transaction will actually be rolled back.
It seems, that I need to create a new transaction using transactionTemplate
and PROPAGATION_REQUIRES_NEW
. I tried this way, and I able to see results of a trigger execution (horray!). But this way creates another problem: look at the code, I tried to explain a strange behavior via comments.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfiguration.class})
@Transactional
public class ServiceTest {
@Inject
private PlatformTransactionManager txManager;
<...>
@Before
public final void setUp() {
clearDatabase();
}
@Test
public final void testFilter() {
// Note: here database is empty - was cleared by setUp() method
TransactionTemplate tt = new TransactionTemplate(txManager);
tt.setPropagationBehavior(PROPAGATION_REQUIRES_NEW);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus s) {
createAndSaveEntity();
}
});
// Here I can see created entity and results of the trigger execution
// But also I see all entities, which was in the database before setUp()
// That is very strange! Why they appear if I deleted them?
<...>
}
}
PS: I use Spring Data Neo4j 4.0.0.RELEASE and Neo4j 2.3.1