无法从数据库中获取元素两次

时间:2014-12-19 22:37:50

标签: java spring hibernate spring-mvc junit

我在Spring MVC + Hibernate + HSQLDB中测试DAO层,我无法从数据库中加载两次播放列表。

我不知道为什么我将List的大小设为0.方法testSavePlaylist成功,但方法test_getPlaylistById失败。当我只测试一种方法时,它工作得很好。

错误追踪:

java.lang.AssertionError: expected:<2> but was:<0>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:555)
at org.junit.Assert.assertEquals(Assert.java:542)
at adrian.example.musicplayer.dao.music.PlayListImplDaoTest.test_getPlaylistById(PlayListImplDaoTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Junit Class

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
@Transactional
public class PlayListImplDaoTest {

    @Autowired
    PlayListDao playlistDao;

    private static boolean setUpIsDone = false;

    @Before
    public  void setUp() throws Exception {
        if(setUpIsDone) {
            return;
        }
        this.playlistDao.savePlaylistForJunit();

        setUpIsDone = true;
    }

    @Test
    public void  test_getPlaylistById() throws InterruptedException {
        List<Playlist> testedPlaylist = (List<Playlist>) this.playlistDao.getPlaylistById(1);
        assertEquals(2, testedPlaylist.size());
    }

    @Test
    public void testSavePlaylist() throws InterruptedException {
        this.playlistDao.savePlaylist(1, "TestPlaylist2");
        List<Playlist> testedPlaylist2 = (List<Playlist>) this.playlistDao.getPlaylistById(1);
        assertEquals(2, testedPlaylist2.size());
    }
}

2 个答案:

答案 0 :(得分:3)

我相信,spring中的事务JUnit测试的默认行为是在每次测试后回滚。因此,在setUp()中执行的一次性DB填充会在第一次测试后回滚,导致第二次测试失败,因为数据库现在为空。

您可以通过使用@TransactionConfiguration(defaultRollback=false)注释测试类来扭转此行为。

但是,进行有状态测试可能被认为是一种不好的做法,即数据在一次测试后持续存在,可能会影响下一次测试的结果,并根据执行顺序产生不同的结果。

相反,您可能最好从setUp()中移除“if”并重新为每个测试通道重新填充数据库。

答案 1 :(得分:3)

您将类注释为@Transactional,这意味着每个@Test方法都将在事务中执行。用@Before注释的方法在每个测试方法之前执行,但在与测试方法相同的事务中执行。请注意@TransactionConfiguration(defaultRollback = true)是测试环境的默认值。因此,如果您运行测试,可能会出现以下情况之一:

  1. 新交易已开启
  2. 执行
  3. 整个setUp()表示使用this.playlistDao.savePlaylistForJunit()创建一些测试数据,之后标志setUpIsDone设置为true
  4. 由于有可用的测试数据,
  5. testSavePlaylist()会成功执行
  6. 事务被回滚,这意味着this.playlistDao.savePlaylistForJunit()创建的测试数据已消失
  7. 新交易已开启
  8. setUp方法提前返回,因为此时setUpIsDone为真
  9. test_getPlaylistById()已执行,但没有可用的测试数据
  10. 交易回滚
  11. 有关详细信息,请查看the section Spring Testing Annotations of the Spring Reference Documentation