我的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
添加到测试方法中(如果希望所有测试方法都具有事务性,则将其添加到测试类中)。