且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JUnit 中使用 Spring 测试服务时如何回滚数据库事务?

更新时间:2022-10-21 15:59:39

您需要将事务边界扩展到测试方法的边界.您可以通过将您的测试方法(或整个测试类)注释为 @Transactional 来实现:

@Test@交易公共无效测试插入(){long id=myService.addPerson("JUNIT");assertNotNull(id);如果(id

您也可以使用这种方法来确保在回滚之前正确写入数据:

@Autowired SessionFactory sf;@测试@交易公共无效测试插入(){myService.addPerson("JUNIT");sf.getCurrentSession().flush();sf.getCurrentSession().doWork(...检查数据库状态...);}

I have no problem testing my DAO and services, but when I test INSERTs or UPDATEs I want to rollback the transaction and not effect my database.

I'm using @Transactional inside my services to manage transactions. I want to know, is it possible to know if a transaction will be fine, but rollback it to prevent altering database?

This is my Test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring.cfg.xml")
@TransactionConfiguration(defaultRollback=true)
public class MyServiceTest extends AbstractJUnit38SpringContextTests  {
    @Autowired
    private MyService myService;

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Test
    public void testInsert(){
        long id = myService.addPerson( "JUNIT" );
        assertNotNull( id );
        if( id < 1 ){
            fail();
        }
    }
}

The problem is that this test will fail because transaction was rollbacked, but the insert is OK! If I remove @TransactionConfiguration(defaultRollback=true) then the test pass but a new record will be inserted into database.

@Test
@Transactional
@Rollback(true)
public void testInsert(){
    long id = myService.addPerson( "JUNIT" );
assertNotNull(id);
if( id < 1 ){
        fail();
    }
}

Now can test pass correctly, but rollback is ignored and the record is inserted into the database. I have annotated the method addPerson() inside myService with @Transactional, obviously. Why is the rollback being ignored?

You need to extend transaction boundaries to the boundaries of your test method. You can do it by annotating your test method (or the whole test class) as @Transactional:

@Test 
@Transactional
public void testInsert(){ 
    long id=myService.addPerson("JUNIT"); 
    assertNotNull(id); 
    if(id<1){ 
        fail(); 
    } 
} 

You can also use this approach to ensure that data was correctly written before rollback:

@Autowired SessionFactory sf;

@Test 
@Transactional
public void testInsert(){ 
    myService.addPerson("JUNIT"); 
    sf.getCurrentSession().flush();
    sf.getCurrentSession().doWork( ... check database state ... ); 
}