自动用随机数据填充原始属性?

时间:2011-08-15 16:30:37

标签: java testing

是否有一个java库可以帮助创建用于测试的类实例?一个检查bean的属性并用随机数据填充它 我基本上是在为C#寻找等价于Object Hydrator的Java。

7 个答案:

答案 0 :(得分:47)

您可以使用PoDaM

PodamFactory factory = new PodamFactoryImpl();
Pojo myPojo = factory.manufacturePojo(Pojo.class);

答案 1 :(得分:19)

看看Random Beans:

https://github.com/benas/random-beans

它允许您使用随机数据填充Java对象图。

希望它有所帮助 此致

答案 2 :(得分:13)

我不知道一个框架,但是自己编写其中一个框架非常简单。复杂性来自非简单属性,即对象关联。像这样的东西处理基础知识然后一些:

public static void randomlyPopulateFields(Object object) {
    new RandomValueFieldPopulator().populate(object);
}

public static class RandomValueFieldPopulator {
    public void populate(Object object) {
        ReflectionUtils.doWithFields(object.getClass(), new RandomValueFieldSetterCallback(object));
    }

    private static class RandomValueFieldSetterCallback implements FieldCallback {
        private Object targetObject;

        public RandomValueFieldSetterCallback(Object targetObject) {
            this.targetObject = targetObject;
        }

        @Override
        public void doWith(Field field) throws IllegalAccessException {
            Class<?> fieldType = field.getType();
            if (!Modifier.isFinal(field.getModifiers())) {
                Object value = generateRandomValue(fieldType, new WarnOnCantGenerateValueHandler(field));
                if (!value.equals(UNGENERATED_VALUE_MARKER)) {
                    ReflectionUtils.makeAccessible(field);
                    field.set(targetObject, value);
                }
            }
        }
    }
}

public static Object generateRandomValue(Class<?> fieldType, CantGenerateValueHandler cantGenerateValueHandler) {
    if (fieldType.equals(String.class)) {
        return UUID.randomUUID().toString();
    } else if (Date.class.isAssignableFrom(fieldType)) {
        return new Date(System.currentTimeMillis() - random.nextInt(DATE_WINDOW_MILLIS));
    } else if (Number.class.isAssignableFrom(fieldType)) {
        return random.nextInt(Byte.MAX_VALUE) + 1;
    } else if (fieldType.equals(Integer.TYPE)) {
        return random.nextInt();
    } else if (fieldType.equals(Long.TYPE)) {
        return random.nextInt();
    } else if (Enum.class.isAssignableFrom(fieldType)) {
        Object[] enumValues = fieldType.getEnumConstants();
        return enumValues[random.nextInt(enumValues.length)];
    } else {
        return cantGenerateValueHandler.handle();
    }
}

答案 3 :(得分:5)

您可以结帐randomizer以获取随机数据。此库有助于从给定的Model类创建随机数据。请查看以下示例代码。

public class Person {

   @FirstName
   String mFirstName;

   @LastName
   String mLastName;

   @Number(min = 14,max = 25,decimals = 0)
   int age;

   @DateValue( from = "01 Jan 1990",to = "31 Dec 2002" , customFormat = "dd MMM yyyy")
   String dateOfBirth;

   @Email
   String  mEmailId;

   @StreetAddress
   public String streetAddress;

   @State
   public String state;

   //Person can have minimum 1 Phone number and maximum 3 phone number
   @Phone(country = Phone.Country.INDIA)
   @CollectionDescriptor(min = 1,max = 3) 
   List<String> phones;

}

//Generate random 100 Person(Model Class) object 
Generator<Person> generator = new Generator<>(Person.class);  
List<Person> persons = generator.generate(100);                          

由于可以使用注释访问许多内置数据生成器,您还可以构建自定义数据生成器。我建议您浏览库页面上提供的文档。

答案 4 :(得分:5)

https://github.com/benas/random-beans为我做了这项工作,而PoDam则以#34;流利的&#34; Ryan Stewart的setter和回答不完整,因为复制粘贴引用了未公开的类! 使用随机bean,它很简单:

Auction auction = EnhancedRandom.random(Auction.class);

摇篮:

testCompile ('io.github.benas:random-beans:3.4.0')

答案 5 :(得分:1)

为了进行测试,我们的团队在JUnit和Mockito上取得了一些成功。这是link to a Mockito answer

我不确定填充随机数据是否是一项有意义的测试。也许更有意义的测试是测试正常,边界和错误条件。

答案 6 :(得分:0)

我在这里使用了反射和递归,以便在我想要测试的健壮对象中填充所有字段。这也是使用PODAM,我希望有人会觉得这很有用。

public class Populate {

    private final PodamFactory podamFactory = new PodamFactoryImpl();

    private <P> P getManufacturedPojo(final Class<P> 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<Field> allFields = getAllFields(targetClass, Predicates.<Field>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<Object> list = new ArrayList<>();
                    list.add(populateAllIn(ClassExtendingAbstract.class));
                    field.set(target, list);

                } else {
                    final List<Object> 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);
}

谢谢,如果有更简单的方法来填充所有内容,请告诉我。