gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:58:06 26 4
gpt4 key购买 nike

我正在 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 类

@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());
}
}

最佳答案

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

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

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

相反,您最好从 setUp() 中删除“if”,并为每次测试通过重新填充数据库。

关于java - 无法从数据库中获取元素两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27574874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com