如何填写班级中的所有字段? [英] How to fill all the fields in the class?
问题描述
假设我有一个包含许多不同字段的类。这个类是DTO,出于测试目的,我不关心实际值,只是它存在。是否有任何工具可以遍历所有字段并设置基元,0表示Number(0.0表示Float,Double,0表示整数,0L表示Long,但默认情况下不为null),类似于testfor String?
Suppose I have a class that has a lot of different fields. This class is a DTO and for testing purposes, I do not care about actual values, just it exists. Is there any tool that can traverse through all fields and set primitives, 0 for Number (0.0 for Float, Double, 0 for Integer, 0L for Long, but not null as default), something like "test" for String?
另外,我希望该工具填充集合(列表,集合,映射)。
Also, I want that tool to populate Collections (List, Set, Map).
推荐答案
只是一个小的谷歌搜索提供这个结果:
Just a small googling provide this results:
- PODAM (主站点)
- JPopulator (Java 6)或其继承者随机豆子(Java 8)
- PODAM (Main site)
- JPopulator (Java 6) or its successor Random Beans (Java 8)
否则你可以使用反射来填充:
else you can use reflection to populate:
- primitive / wra pper具有默认值
- 具有rendom值的字符串
-
具有随机大小的集合(set,list)并将代码重新用于
填充
- primitive/wrapper with default value
- string with rendom value
Collection (set,list) with random size and re-using code to populate
依旧。
其他XML绑定(使用jaxb或其他技术)可以是一个选项,但需要提前准备xml数据。
除了框架之外,所有其他解决方案都有两大问题:需要编写和很多测试!
Else XML binding (with jaxb or other technology) can be an option but needs to prepare xml with data in advance.
Except frameworks all other solutions have two big issues: needs to be written and a lot of testing!
这篇关于如何填写班级中的所有字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!