为单元测试用例设置批量数据
我使用 Spring MVC 开发了一个应用程序,用于处理批量数据插入/更新。例如:有些用例会插入具有 100-125 个属性的记录。
对于批量数据插入,我对要插入到单元测试类中的值进行硬编码。我有传输对象来携带数据,因此我在单元测试用例中填充这些 TO,然后调用要测试的所需操作。对于每个 DAO,至少需要 4 个测试用例来对 CRUD 操作进行单元测试。
我发现通过对测试用例源文件中的值进行硬编码来填充 TO 非常困难。想象一下为每个单元测试用例编写 125 个 setter。我想通过从 XML 文件或任何类型的媒体读取数据来动态填充我的 TO,这样我就不需要每次都更改测试用例的硬编码数据。
在 XML 文件中设置数据比在 JUNIT 源文件中对值进行硬编码要容易得多。
我可以想到一些创新的解决方案,例如在 XML 文件中设置数据,然后使用任何 JAXB 实现来读取数据并填充 TO。但我相信有一些简单且更好的方法来处理此类需求。
在这方面需要一些帮助。
I have developed an application with Spring MVC that deals with bulk data insert/update. For ex: There are some use cases that insert a record with 100-125 attributes.
For bulk data insert, I'm hard coding the values to be inserted in my Unit test class. I have Transfer Object to carry the data, so i'm populating these TOs in my Unit test cases and then calling the desired operation to be tested. For every DAO, at least 4 test cases are needed for unit testing CRUD operations.
I'm finding it very hard to populate the TOs by hard coding the values in my test case source file. Imaging writing 125 setters for every unit test case. I want to populate my TOs dynamically by reading the data from an XML file or any kind of media, so that I need not change the hard coded data for test cases everytime.
Setting up the data in an XML file is much easier than hard coding the values in JUNIT source files.
I could think of some innovative solutions like setting up data in XML file and then using any JAXB implementation to read the same and populate the TOs.. but i believe there much be some easy and better way to handle this kind of requirement.
Need some help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要处理测试数据的测试辅助方法。然后,您调用填充 TO 的适当方法:它可以少至 0 个参数(完全随机/固定,非测试驱动),也可以多至 125 个参数(完全由测试控制)(以及介于两者之间的任何参数)。
不管怎样,测试中不再有设置者了。
You need test helper method(s) that take care of test data. Then you call appropriate method that populates a TO: it could be as little as 0 parameters (completely random/fixed, not test driven) or as many as 125 (completely controlled by the test) (and anything in between).
Either way no setters in tests anymore.
也许你可以使用这个框架:
http://jtestcase.sourceforge.net
Maybe you could use this framework:
http://jtestcase.sourceforge.net
我不确定 Java 单元测试框架。但在 DotNet 中有一个名为 NBuilder 的库,它可以帮助您非常轻松快速地创建测试数据。这是我写的博客以展示其能力。
也许您可以尝试看看是否存在 NBuilder 的 Java 替代品,或者 Java 中是否有相同的端口。我发现的大多数 DotNet 库都是从 Java 世界移植的,如 NHibernate、Spring.Net 等。所以我的猜测是,您可能也有一个相当于 NBuilder 的 Java 版本。
I am not sure about the Java unit testing frameworks. But in DotNet there is a library called NBuilder which helps you to create test data very easily and quckly. Here is a blog I had written to demonstrate its capabilities.
May be you can try and see if there exists an Java alternative to NBuilder or there is a port of the same available in Java. Most of the DotNet libraries I find are ported from Java world like NHibernate, Spring.Net etc. So my guess is that you might have a Java equivalent of NBuilder as well.