我正在进行单元测试,它通过DAO向Oracle数据库提交信息,然后检索它并检查一切是否未更改。@NotTransactional alternatives
有在测试类中其他的单元测试,并在类的顶部,我有:
@TransactionConfiguration (defaultRollback = true)
我想知道我怎么能删除@NotTransactional。我没有在类中指定Transactional,所以默认情况下没有测试应该是这样的。由于这是自己的测试,我不知道@BeforeTransaction(或After)注释是否正确。
最大的问题是没有@NotTransactional,它看起来好像unsubscribe()函数没有运行。 (垃圾标志未更改)
再次运行测试,使用@ rollback = false和@NonTransactional,我在数据库中看到垃圾标志正确设置为true,测试完成后数据库中。
@RunWith (SpringJUnit4ClassRunner.class)
@TransactionConfiguration (defaultRollback = true)
public class DatabaseTest {
@Autowired (required = true)
private FooDao<Foo> fooDao;
@Autowired (required = true)
private FooService fooService;
// other tests here that are marked @Transactional.
@Test
@NotTransactional
public void testDelete() {
Foo foo = new foo();
foo.setTrashFlag(false);
foo.setId(123);
fooDao.create(foo);
Foo fooFromDb = fooService.getFromDbById(123);
assertNotNull(fooFromDb);
fooService.unsubscribe(123); // among other things,
// **sets trash flag to true.**
// sqlSession.flushStatements(); // doesn't seem to commit the unsubscribe action
// getFromDbById() returns ONLY Foos with trash set to false, so we expect
// nothing returned here as we've just set trash to true, using unsubscribe().
Foo trashedFoo = fooService.getFromDbById(123);
assertNull(trashedFoo);
谢谢!
您应该可以阅读您在同一交易中写下的任何记录。 – Marvo 2013-03-18 23:18:08
我同意。我仍然无法确定为什么它不会在测试方法上没有执行@NotTransactional。 – iank 2013-03-19 15:55:16
我承认我从来没有听说过NotTransactional,直到你的问题。你能否展示你的一个测试并解释你期望发生的事情?如果我需要访问Hibernate对象上的子记录,我只需将@Transactional放在我的测试中,这对我很有用。 – Marvo 2013-03-19 18:46:40