@Before 和 @After 排除
我正在为我的 DAO 编写一些测试,因为很多测试使用保存到我的数据库中的测试对象,所以我创建了一个 setup() 和teardown() 方法,分别带有注释 @Before 和 @After为了避免冗余代码,但其中一个测试(实际上并不真正需要测试对象)调用 DAO 中的一个方法,其中包含调用 getCurrentSession().clear() (这是一种使用 ScrollableResults 获取数据的方法批量从数据库中读取,为了避免内存被填满,它每 50 行调用一次lush()和clear()。这会产生一个问题,因为clear()实际上从会话中删除了在setup()中创建的测试对象,所以当调用teardown()时我得到一个错误:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [nl.ru.cmbi.pdbeter.core.model.domain.PDBEntry#395]
Is there a way to sell JUnit to not use the setup () 和teardown() 在此测试中,还是将所有实际上不需要setup() 和teardown() 的测试放在一个新的测试类中更好?
I'm writing some tests for my DAO, and because a lot of the tests use a test object that is being saved to my database I've created a setup() and teardown() method with the annotations @Before and @After respectively to avoid redundant code, but one of the tests, actually one that doesn't really need the test object, calls a method in the DAO that contains the call getCurrentSession().clear() (it's a method that uses ScrollableResults to get data from the db in batches, and to avoid the memory to fill up it calls flush() and clear() every 50 rows). This creates a problem, because the clear() actually removes the test object that is created in setup() from the session, so when teardown() is called I get an error:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [nl.ru.cmbi.pdbeter.core.model.domain.PDBEntry#395]
Is there a way to tell JUnit to not use the setup() and teardown() on this test, or is it better to put all the tests that don't actually need the setup() and teardown() in a new test class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,是的,在单独的测试中隔离不需要
@Before
或@After
行为的测试非常有意义。其次,您可能想看看 Spring 框架支持在数据库事务中运行单元测试,该事务会在每次测试结束时自动回滚,这样您就不必担心某些测试会影响数据库的状态外部资源,或者询问每个订单正在运行哪些测试等问题。将其与内存数据库结合起来(例如 HSQL< /a>),您甚至不需要担心在某个地方有一个正在运行的数据库来运行,这使得您的构建更加可移植。
First of all yes, it makes a lot of sense to isolate tests that don't need the
@Before
or@After
behavior in a separate test.Secondly, you may want to take a look at the Spring Framework's support for running unit tests within a database transaction that is automatically rolled back at the end of each test, so that you do not have to ever worry about some tests affecting the state of an external resource, or ask questions about which tests are being run in each order, etc. Combine this with an in-memory database (like HSQL) and you won't even need to worry about having a running database somewhere to run against, making your build much more portable.
JUnit 将为每个测试运行所有用
@Before
和@After
注释的方法,因此您需要将测试分成两个类。JUnit will run all methods annotated with
@Before
and@After
for each test so you will need to split your tests into two classes.定义您自己的 BlockJUnit4ClassRunner:
然后在您的测试用例中使用它:
至少它在 JUnit 4.8 中可用。
Define your own BlockJUnit4ClassRunner:
Then use it in your test case:
At least it is available in JUnit 4.8.