我对 Spring/Hibernate 的 DAO 类管理器使用 TestNG 进行了集成测试。如下所示。我试图让一个测试插入数据,然后另一个测试只是为了测试 fetch 方法。所以我使用了TestNG提供的dependsOn。但是现在的问题是,一旦我的第一个测试结束,它就会回滚事务,因为我在 TransactionConfiguration 注释中给出了 defaultRollBack = true。所以我的第二次测试失败了。在课堂上的所有测试完成后,有什么方法可以阻止它并回滚?
import static org.testng.Assert.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.Test;
import com.worksheet.State;
@ContextConfiguration("classpath*:spring-context-test.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class StateDAOTest extends AbstractTransactionalTestNGSpringContextTests{
@Autowired
private StateDAO stateDAO;
@Test
public void saveUnknownState(){
State state = new State("Unknown", "UKN");
stateDAO.saveState(state);
assertTrue(state.getId() > 0);
}
@Test(dependsOnMethods = {"saveUnknownState"})
public void fetchUnknownState(){
State state = stateDAO.getStateByAbbreviation("UKN");
assertNotNull(state);
}
}