如何测试具有不可空集合的持久实体
我正在学习如何编写测试用例,但遇到了问题。 我有一个带有entityA 的持久单元,它没有空的B 集合。 应用程序工作流程确保 B 永远不会为空并且包含已持久化的实体。 (假设用户必须从他的地址簿中选择电子邮件地址)
现在我在测试持久化 A 时遇到问题。 如果我创建 A,我必须创建一组 B(@NotNull)。 B 在测试数据库中不存在,因此我收到错误“对象引用未保存的瞬态实例”。
我正在使用 dbunit 所以问题是: 是否可以使用entitymanager并选择一组已从xml文件加载进行测试的B,然后将它们添加到A并保留A?对我来说,这听起来不像单元测试。处理这种情况的最佳做法是什么
谢谢
I'm learning how to write test cases and I've encounter a problem.
I have a persistence unit with entityA that has not empty collection of Bs.
The application workflow ensures that B is never empty and contains entities that have already been persisted.
(Let's say a user has to choose email addresses from his address book)
Now I have a problem in testing persisting A.
If I create A i must create a set of Bs (@NotNull).
B doesn't exist in test db so I get an error "object references an unsaved transient instance".
I'm using dbunit so the question is:
Is it all right to use entitymanager and select set of Bs, that have been loaded for test from xml files, then add them to A and persist A? It somehow doesn't sound like a unit test to me. What is the best practice to handle this situation
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我看不到任何其他可能性。 A 对 B 具有依赖关系,因此您需要拥有 B 才能持久保存 A。由于在这种情况下不可能进行模拟,因此您需要从数据库中获取 B。
I don't see any other possibility. A has a dependency to B, so you need to have Bs in order to persist A. Since mocking is not possible in this case, you need to get your Bs from the database.