0

I got a problem with my 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());
}

 }

Every time I run my test class the data is saved permanently. But I don't want that for my test classes.

I don't see the problem.

Charles
  • 50,943
  • 13
  • 104
  • 142
nano_nano
  • 12,351
  • 8
  • 55
  • 83

1 Answers1

2

Your tests are not transactional, so Spring doesn't have any transaction to rollback.

Add @Transactional to the test methods (or to the test class if you want all its test methods to be transactional).

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • you are right. with Transactional it works. Thx a lot. Just for interest, for what is @TransactionConfiguration? – nano_nano Oct 05 '13 at 10:58
  • To specify how Spring must handle transactions of transactional tests: which TxManager to use, does it have to rollback or commit. – JB Nizet Oct 05 '13 at 11:02