我在这里使用反射和递归来获取我要测试的坚固对象中的所有字段。 我也正在使用PODAM,我希望有人会发现它有用。
public class Populate {
private final PodamFactory podamFactory = new PodamFactoryImpl();
private
P getManufacturedPojo(final Class
klass) {
return podamFactory.manufacturePojo(klass);
private Object populateAllIn(final Class targetClass) throws IllegalAccessException, InstantiationException {
final Object target = targetClass.newInstance();
//Get all fields present on the target class
final Set allFields = getAllFields(targetClass, Predicates.alwaysTrue());
//Iterate through fields
for (final Field field : allFields) {
//Set fields to be accessible even when private
field.setAccessible(true);
final Class> fieldType = field.getType();
if (fieldType.isEnum() && EnrichmentType.class.isAssignableFrom(fieldType)) {
//handle any enums here if you have any
//Check if the field is a collection
if (Collection.class.isAssignableFrom(fieldType)) {
//Get the generic type class of the collection
final Class> genericClass = getGenericClass(field);
//Check if the generic type of a list is abstract
if (Modifier.isAbstract(genericClass.getModifiers())) {
//You might want to use any class that extends
//Your abstract class like
final List list = new ArrayList<>();
list.add(populateAllIn(ClassExtendingAbstract.class));
field.set(target, list);
} else {
final List list = new ArrayList<>();
list.add(populateAllIn(genericClass));
field.set(target, list);
} else if ((isSimpleType(fieldType) || isSimplePrimitiveWrapperType(fieldType)) && !fieldType.isEnum()) {
field.set(target, getManufacturedPojo(fieldType));
} else if (!fieldType.isEnum()) {
field.set(target, populateAllIn(fieldType));
return target;
和一些辅助方法。 代码可能并不完美,但是可以工作:)。
private Class> getGenericClass(final Field field) {
final ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
return (Class>) collectionType.getActualTypeArguments()[0];
private boolean isSimpleType(final Class> fieldType) {
return fieldType.isPrimitive()
|| fieldType.isEnum()
|| String.class.isAssignableFrom(fieldType)
|| Date.class.isAssignableFrom(fieldType);
private boolean isSimplePrimitiveWrapperType(final Class> fieldType) {
return Integer.class.isAssignableFrom(fieldType)
|| Boolean.class.isAssignableFrom(fieldType)
|| Character.class.isAssignableFrom(fieldType)
|| Long.class.isAssignableFrom(fieldType)
|| Short.class.isAssignableFrom(fieldType)
|| Double.class.isAssignableFrom(fieldType)
|| Float.class.isAssignableFrom(fieldType)
|| Byte.class.isAssignableFrom(fieldType);
谢谢,如果有更简单的方法来填充所有内容,请告诉我。
我在这里使用反射和递归来获取我要测试的坚固对象中的所有字段。 我也正在使用PODAM,我希望有人会发现它有用。public class Populate {private final PodamFactory podamFactory = new PodamFactoryImpl();private P getManufacturedPojo(final Class klass) {return...
开发中常常遇到需要Mock数据来测试功能是否完整,手动一个一个填数据有点浪费时间,最近找到这样一个小工具,非常方便的给Pojo类所有字段填上随机值,非常好用,这里推荐下。
官网:https://mtedone.github.io/podam/
使用
方法:
1、添加Maven依赖
<dependency>
<groupId>uk.co.jemos.podam</groupId>
<artifactId>podam</artifactId>
**编写Person类 **
属性
:String name, int id;方法:必要的构造方法,
属性
name的getter/setter方法,覆盖Object中继承的toString方法(返回的字符串格式:id=id的值 name='name的值')
ObjectGenerator的getObject可以根据给定的种子随机生成String、Integer、Person对象。
单元测试可以有效地测试某个程序模块的行为,是未来重构代码的信心保证。
单元测试的测试用例要覆盖常用的输入组合、边界条件和异常。
单元测试代码要非常简单,如果测试代码太复杂,那么测试代码本身就可能有bug。
单元测试通过了并不意味着程序就没有bug了,但是不通过程序肯定有bug。
2 测试框架
Junit+mockito+assertj
如果您需要用虚拟数据
填充
pojo对象,并且您真的想避免像这样的大量按键操作
bean . setPropertyA( ... );
bean . setPropertyB( ... );
bean . setPropertyC( ... );
bean . setPropertyD( ... );
bean . setPropertyE( ... );
bean . setPropertyZ( ... );
使用
@Randomize批注标记测试的实例字段@Randomize 。 基本思路如下
@Randomize private Pojo bean;
@Test public void thisIsAtest(){
bean . getPropertyA() // null-saf
自动
生成测试类,并
填充
测试数据(一)一 说明二 设计思路三 上代码1 工具类 GenStrUtil2 生成类 GenServiceTest四 结语五 感谢关注
在日常开发过程中,需要对功能做单元测试,还需要制造测试数据。在测试的过程中,造数是比较蛋疼的事情,并且需要编写大量的set方法。所以就有感制作一个
自动
生成测试用例的代码。
二 设计思路
内容其实很简单,就是通过固定的字符串拼接 + 反射机制来实现功能。其中有几个需要注意的地方是,判断基本数据类型,List,Map,存在泛型的类,这些是否有.