对对象图中的孤立对象进行单元测试

发布于 2025-01-06 13:34:15 字数 404 浏览 0 评论 0原文

我正在尝试为在遗留数据库之上构建的 JPA 模型编写一些单元测试。我的模型中的一些对象图很大,我想找到一种编写单元测试的方法,以便我可以测试图中的单个对象(即实例化新对象、填充数据、持久化、刷新、比较刷新)对象成员与原始)隔离,而不必完全填充对象图。

我最初的想法是,我可以对被测对象中的任何引用对象使用模拟对象,但我认为这还不够,因为模式的引用完整性不允许模拟对象表示的对象无需填充数据即可插入。

我想我真正想要的是一种实例化被测试对象的方法,然后用我并不真正关心的数据填充对象图的其余部分,但仍然保持模式的引用完整性。

我知道 Java 单元测试有很多选择,包括 DbUnit、EasyMock、Unitils 等,但我不确定应该使用什么组合(或功能)来解决这个问题。有人能指出我正确的方向吗?也请随意批评我解决问题的方法;我可能需要重组它。

I'm trying to write some unit tests for a JPA model that I've built on top of a legacy database. Some of the object graphs in my model are large and I would like to find a way to write the unit tests such that I can test a single object in the graph (i.e. instantiate new object, populate with data, persist, refresh, compare refreshed object members with original) in isolation without having to fully populate the object graph.

My initial thought was that I could use mock objects for any referenced objects in the object under test but I don't think this will be sufficient being that the referential integrity of the schema will not allow the objects that are represented by the mock objects to be inserted without being populated with data.

I guess what I am really looking for is a way to instantiate the object under test and then have the rest of the object graph populated somehow with data that I don't really care about but that still maintains the referential integrity of the schema.

I know there are many options out there for Java unit testing including DbUnit, EasyMock, Unitils etc. but I'm not sure exactly what combination (or features) I should use to solve this problem. Can someone point me in the right direction? Feel free to criticize my approach to the problem as well; I may need to restructure that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-01-13 13:34:15

您可能想查看对象母体或测试数据生成器模式;

TestDataBuilder 模式允许测试仅指定那些部分
需要改变的对象并为这些对象使用合理的默认值
与测试无关的内容。

另请参阅:

You might want to look at the Object Mother or Test Data Builder patterns;

The TestDataBuilder pattern allows tests to specify only those parts
of the objects that need to vary and use sensible defaults for those
that are not relevant to the test.

see also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文