We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
我还寻找了 Factory Girl 的 Java 版本,但从未找到类似的东西。 相反,我从头开始创建了一个解决方案。 用于用 Java 生成模型的工厂:Model Citizen。
受 Factory Girl 的启发,它使用字段注释来设置模型的默认值,一个简单的示例来自 wiki:
这将是汽车模型的蓝图。 这已注册到 ModelFactory 中,然后可以按如下方式创建新实例:
您可以通过传入 Car 实例而不是 Class 并根据需要设置值来覆盖 Car 模型的值:
wiki 有更复杂的示例(例如使用 @Mapped)以及更多附加功能的详细信息。
I also looked for a Java equivalent of Factory Girl, but never found anything like it. Instead, I created a solution from scratch. A factory for generating models in Java: Model Citizen.
Inspired by Factory Girl, it uses field annotations to set defaults for a Model, a simple example from the wiki:
This would be the Blueprint for the Car model. This is registered into the ModelFactory, than new instances can be created as follows:
You can override the values of the Car model by passing in an instance of Car instead of the Class and setting values as needed:
The wiki has more complex examples (such as using @Mapped) and details for a few more bells and whistles.
一个可能执行此操作的库是 Usurper。
然而,如果您想指定正在创建的对象的属性,那么 Java 的静态类型会使框架变得毫无意义。 您必须将属性名称指定为字符串,以便框架可以使用反射或 Java Bean 自省来查找属性访问器。 这将使重构变得更加困难。
只需新建对象并调用它们的方法就简单得多。 如果您想在测试中避免大量样板代码,测试数据生成器 模式可以提供帮助。
One possible library for doing this is Usurper.
However, if you want to specify properties of the objects you are creating, then Java's static typing makes a framework pointless. You'd have to specify the property names as strings so that the framework could look up property accessors using reflection or Java Bean introspection. That would make refactoring much more difficult.
It's much simpler to just new up the objects and call their methods. If you want to avoid lots of boilerplate code in tests, the Test Data Builder pattern can help.
我知道这并不完全是您正在寻找的...
过去我编写了一些使用反射来填充 beans 值的代码。 基本思想是找到所有设置器并使用虚拟值调用每个设置器。 我的版本将所有字符串设置为将使用“name”调用的字段 setName 的名称,然后将所有整数设置为 1,将布尔值设置为 true 等。
然后我将其与类似于对象母体和测试数据生成器的模式结合使用。
它为测试数据提供了良好的开端,任何需要特定值的字段都可以作为测试的一部分明确设置。
希望这可以帮助。
I know this isn't exactly what you are looking for...
I the past I've written some code that using reflection to populate a beans values. The basic idea is to find all the setters and call each with a dummy value. My version set all Strings as the name of the field setName would be called with "name", then set all ints as 1, booleans to true etc.
I then used this in conjuction with the patterns similar to Object mother and Test Data Builder.
It provided a good start for test data and any fields that required specific values could be set explicitly as part of the test.
Hope this helps.
我带着同样的问题来到这里,需要数据生成工具来进行集成测试。 我决定接受 Groovy 的挑战,它是我选择的测试语言,因为它的紧凑性和 电源断言。
我刚刚写了一个小助手 https://gist.github.com/pgaertig/9502960称为 FactoryGrill ;)它允许您编写如下所示的数据脚本。
上面相当于:
你可以用 Groovy 做更多的事情:
它实际上不是工厂,因为工厂在 Groovy 中非常简单 地图构造函数 或 展开。
FactoryGirl 的参考资料和其他内容目前不可用,因为上述内容仅需要约 30LOC 才能实现。 但是,如果对我的解决方案感兴趣,我将在 Github 上添加一个专门的项目。
I arrived here with the same question and needed for data generation tool for my integration tests. I decided to accept the challenge with Groovy, which is my language of choice for tests because of its compactness and power assert.
I've just written a small helper https://gist.github.com/pgaertig/9502960 called FactoryGrill ;) which allows you to write data scripts like below.
above is equivalent to:
You can do more with Groovy:
It is not factory really because factories are quite straight forward in Groovy with map constructor or expandos.
References and other stuff from FactoryGirl is not available currently because above is achieved only with ~30LOC. However if there is an interest in my solution I will add make a dedicated project on Github.
如果您的模型对象很简单,则没有理由使用框架来创建它们,只需使用“new”运算符即可。
如果你有复杂的模型(复杂的关系),那么你可以使用 spring 将它们绑定在一起(即使在测试场景中你也可以使用 spring),
If your model objects are simple, there is not reason to use a framework to create them, just simply use 'new' operator.
If you have complex model (complex relationships) then you can use spring to bind them together (even in test scenarios you can use spring)