我正在做一个单元测试,它通过 DAO 将信息提交到 Oracle 数据库,然后检索它并检查一切都没有改变。
测试类中还有其他单元测试,在类的顶部我有:
@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);
谢谢!