java - Spring Dao-Test defaultRollback 不起作用

标签 java spring junit spring-junit

我的 Dao-Test 遇到问题:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/cmn-dao-spring.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class ScoreDaoTest extends TestCase {

@Autowired
private ScoreDao mScoreDao;

@Autowired
private ScoreCreator mScoreCreator;

@Autowired
private QuestionCreator mQuestionCreator;

@Override
protected void setUp() throws Exception {
    super.setUp();
}

@Test
public void testLoadAllScore() throws Exception {
    List<Score> lAllScore = mScoreDao.loadAllScore(0, 0);
    Assert.assertTrue(lAllScore.isEmpty());
}

@Test
public void testSaveScore() throws Exception {
    Question question = mQuestionCreator.createQuestion(49954854L, new Date(), "Wer bist Du?", "Jörg", "Anja", "Stefan", "Willi", 3, true, false, 1, "DE", "DE_QZ");
    Assert.assertNotNull(question);
    mScoreDao.saveScore(mScoreCreator.createScore(-1L, null, "Stefan", 1033, 27, "Wuhuuuu", question));
    List<Score> lAllScore = mScoreDao.loadAllScore(0, 1);
    Assert.assertFalse(lAllScore.isEmpty());
}

 }

每次运行测试类时,数据都会永久保存。但我不想在我的测试课上这样。

我没有看到问题。

最佳答案

您的测试不是事务性的,因此 Spring 没有任何要回滚的事务。

@Transactional添加到测试方法(或者如果您希望其所有测试方法都是事务性的,则添加到测试类)。

关于java - Spring Dao-Test defaultRollback 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19196935/

相关文章:

java - 通过返回值更改字段与通过在方法中引用字段来更改字段?

spring - 如何以功能方式在@RefreshScope 中注册 spring bean?

java - Spring Boot启动器安全性未进行身份验证

Java JUnit - 是否可以确定是否抛出异常以及抛出哪个异常?

java - 自定义 Guice 作用域应如何与 TestNG 集成?

spring-boot - IntelliJ : How to set log levels for tests

java - 使用 WebDriver 更改 URL

java - 使用子类名访问父类静态字段不会加载子类?

java - 在无限循环中运行的 Spring Batch 作业

spring - 获取 Spring 3.1 MVC View 的渲染输出