2013-10-03 242 views
1

我必須用隨機值填充給定對象的每個屬性。隨機數據生成器

  1. 所有屬性都是原生的Java類型(intdoubleString等)
  2. 我可以使用反射
  3. 我可以使用Spring DirectFieldAccessor

我不想重新發明方形輪,所以我更願意問。

現在我來到了這一點:
獲取所有屬性與

Field field : myObject.getClass().getDeclaredFields() 

遍歷這些字段名稱,並得到他們的類。

然後爲每個已知的本地Java類型使用一個巨大的switch語句並生成一個隨機值。

您認爲如何?

+2

聽起來很有趣!去吧! –

+1

有趣的方法。就像你想的那樣! :) – SudoRahul

+0

你有任何字段和/或數據類型的特定約束,你的隨機值必須服從是有效的嗎? (例如,數值範圍,字符串的最小/最大長度,字符串中允許的字符,...) – isnot2bad

回答

0

因爲這是用於隨機數據生成的一個randomizer。這僅使用Reflection的概念,就像您提到的那樣。 它檢查字段中提到的註釋,並基於它檢查Provider類。請通過模型類。 它有一些原始的和非原始的fo字段。

public class Person { 

    @FirstName 
    public String mFirstName; 

    @LastName 
    public String mLastName; 

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

    @Email 
    public String mEmailId; 

    @ReferenceRecord(clazz = Address.class) 
    public Address address; 

} 

@ReferencedRecord 
public class Address { 

    @StreetAddress 
    public String streetAddress; 

    @State 
    public String state; 
} 

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

由於有很多內置的數據生成器訪問使用註釋,您亦可以利用@CustomGenerator annotation.I建議你去通過提供的庫頁的文檔,自定義數據生成器。

0

您可以使用一個名爲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