您可以使用名为MockNeat的库以编程方式用可以作为“真实”传递的任意数据“填充”您的对象。
例如,为了填充一个对象,您可以查看一下reflect()方法:
// Creates a MockNeat object that internally uses
// a ThreadLocalRandom.
MockNeat m = MockNeat.threadLocal();
List<Employee> companyEmployees =
m.reflect(Employee.class) // The class we are mocking
.field("uniqueId",
m.uuids()) // Generates a random unique identifier
.field("id",
m.longSeq()) // Generates long numbers in a sequence
.field("fullName",
m.names().full()) // Generates a full name for the employer
.field("companyEmail",
m.emails().domain("company.com")) // Generates a company email with a given domain
.field("personalEmail",
m.emails()) // Generates an arbitrary email without domain constraints
.field("salaryCreditCard",
m.creditCards().types(AMERICAN_EXPRESS, MASTERCARD)) // Generate credit card numbers of 'types'
.field("external",
m.bools().probability(20.0)) // Generates Boolean values with 20% probability of obtaining True
.field("hireDate",
m.localDates().past(of(1999, 1, 1))) // Generatest a date in the past, but greater than 01.01.1987
.field("birthDate",
m.localDates().between(of(1950, 1, 1), of(1994, 1, 1))) // Generates a data in the given range
.field("pcs",
m.reflect(EmployeePC.class) // Mock an EmployeePC object
.field("uuid",
m.uuids()) // Generates an unique identifier
.field("username",
m.users()) // Generates an arbitrary username
.field("operatingSystem",
m.from(new String[]{"Linux", "Windows 10", "Windows 8"})) // Randomly selects an OS from the given List
.field("ipAddress",
m.ipv4s().type(CLASS_B)) // Generates a CLASS B IPv4 Address
.field("macAddress",
m.macs()) // Generates a MAC Address
.list(2)) // Creates a List<EmployeePC> with 2 values
.list(1000) // Creates a List<Employee> with 1000 values
.val(); // Returns the list
后期编辑:
自从我最初的回答以来,已经添加了更多生成数据的方法。