Automatic generation of java test mock object framework DataFactory-01-getting Started tutorial

Keywords: Java Maven JDK github

Project brief introduction

Data-Factory It is used to generate initialization information randomly and automatically according to the object, which avoids the tedious of creating the object manually and is easy to test.

Characteristic

  • 8 basic types of support
  • String, Date, amount, Date and other common types of support
  • Java 8 common time class support
  • Support annotation definition of String and Number types
  • Add @ DataFactory annotation support
  • Regex regular expression support

Change log

Change log

Core class explanation

DataUtil tool class

Provide thread safe methods:

/**
 * Construction result
 * @param clazz type
 * @return Construction result
 */
public static <T> T build(final Class<T> clazz) {
    IData data = getInstance();
    return (T) data.build(null, clazz);
}

/**
 * Construction result
 * @param context Execution context
 * @param clazz type
 * @return Construction result
 */
public static <T> T build(final IContext context, final Class<T> clazz) {
    IData data = getInstance();
    return (T) data.build(context, clazz);
}

Quick start

Preparation

JDK 1.8+

Maven 3.0+

If it is idea, Enable Annotation Processing during test.

Introduction of maven

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>data-factory-core</artifactId>
    <version>0.0.3</version>
</dependency>

Demo object

A normal java object for demonstration.

public class User {

    private String name;

    private int age;

    private Date birthday;

    private List<String> stringList;

    //Enumeration of S/F
    private StatusEnum statusEnum;

    private Map<String, String> map;
    
    //Getter & Setter
}

Use tool class

@Test
public void buildBeanBaseTest() throws Exception {
    User user = DataUtil.build(User.class);
    System.out.println(user);
}

The output information is as follows:

User{name='wZ8CJZtK', age=-564106861, birthday=Wed Feb 27 22:14:34 CST 2019, stringList=[Du4iJkQj], statusEnum=S, map={yA5yDqM=Kdzi}}

Content is random every time. Easy to fill in basic test data.

Expanding reading

DataFactory-01-annotation support

DataFactory-02-regular expression support

Posted by kovudalion on Tue, 03 Dec 2019 02:12:12 -0800