2015-05-18 13 views
6

Đôi khi tôi cần giả lập khá lâu, để viết ra, POJO trong các trường hợp thử nghiệm của tôi. Tôi đã tự hỏi nếu có anyway tôi có thể tạo ra các mocks từ debug dữ liệu biến trong Intellij (14)?Làm thế nào để tạo ra một giả từ gỡ lỗi các giá trị biến trong intellij?

Như một ví dụ, chúng ta có một lớp:

public class MyClass{ 
    private String aVariableWithARatherLongName1; 
    private Double aVariableWithARatherLongName2; 
    private String aVariableWithARatherLongName3; 
    private Long aVariableWithARatherLongName4; 
    private String aVariableWithARatherLongName5; 
    private Boolean aVariableWithARatherLongName6; 
    private String aVariableWithARatherLongName7; 
    private String aVariableWithARatherLongName8; 
    private String aVariableWithARatherLongName9; 
    private String aVariableWithARatherLongName10; 
    private String aVariableWithARatherLongName11; 
    private String aVariableWithARatherLongName12; 

    //getters & setters 
} 

Và trong cái nhìn biến debug của tôi, tôi sẽ có một danh sách MyClass biến:

- myClasses = {[email protected]} size = 5 
    - 0 = {[email protected]} 
     - aVariableWithARatherLongName1 = {String} "value 1" 
     - aVariableWithARatherLongName2 = {Double} 2.0 
     - aVariableWithARatherLongName3 = {String} "value 1" 
     ... 
    - 1 = {[email protected]} 
     - aVariableWithARatherLongName1 = {String} "value 2" 
     - aVariableWithARatherLongName2 = {Double} 2.0 
     - aVariableWithARatherLongName3 = {String} "value 2" 
     ... 
    + 2 = {[email protected]} 
    + 3 = {[email protected]} 
    + 4 = {[email protected]} 

Và sau đó các plugin hoặc Intellij sẽ tạo ra một cái gì đó như dưới đây dựa trên ngôn ngữ đã chọn (Groovy trong ví dụ này):

def mockedResults(){ 
    [ 
     new MyClass(aVariableWithARatherLongName1: 'value 1', aVariableWithARatherLongName2: 2.0, aVariableWithARatherLongName3: 'value 1', ...), 
     new MyClass(aVariableWithARatherLongName1: 'value 2', aVariableWithARatherLongName2: 2.0, aVariableWithARatherLongName3: 'value 2', ...), 
     new MyClass(aVariableWithARatherLongName1: 'value 3', aVariableWithARatherLongName2: 2.0, aVariableWithARatherLongName3: 'value 3', ...), 
     new MyClass(aVariableWithARatherLongName1: 'value 4', aVariableWithARatherLongName2: 2.0, aVariableWithARatherLongName3: 'value 4', ...), 
     new MyClass(aVariableWithARatherLongName1: 'value 5', aVariableWithARatherLongName2: 2.0, aVariableWithARatherLongName3: 'value 5', ...), 
    ] 
} 

Có phải một cái gì đó như thế này có thể với Intellij (14) hoặc có bất kỳ plugin nào cung cấp chức năng như thế này không?

+0

Cùng câu hỏi, tìm kiếm trên web, tìm thấy điều gì Dù sao tôi sẽ đăng ở đây nếu tôi tìm thấy một cái gì đó. – Benj

Trả lời

0

Tôi không biết cách nào để thực hiện điều này theo mặc định. Vì vậy, một cái gì đó giống như mã groovy dưới đây sẽ làm việc. Nó sẽ mất nhiều hơn một chút công việc để có thể xử lý một danh sách các đối tượng như bạn có trong ví dụ của bạn.

public class ObjectBuilder { 
Object object 

private static final Map PREFIXES = [(String.class): "\"", (Integer.class): "", (Boolean.class): "", (Double.class): "", (Long.class): ""] 
private static final Map SUFIXES = [(String.class): "\"", (Integer.class): "", (Boolean.class): "", (Double.class): "d", (Long.class): ""] 

ObjectBuilder(Object object) { 
    this.object = object 
} 

def build() { 
    List properties = [] 
    object.getClass().getDeclaredFields().each { 
     if(!it.isSynthetic()) { 
      String propertyName = it.getName() 
      properties << "$propertyName: ${PREFIXES.get(it.getType())}${object[propertyName]}${SUFIXES.get(it.getType())}" 
     } 
    } 
    println "new ${object.getClass().getSimpleName()}(${properties.join(", ")})" 
} 
} 

Tôi đã thử nghiệm nó với đoạn mã sau (mượn từ MyClass ví dụ của bạn:

MyClass myClass1 = new MyClass() 
myClass1.aVariableWithARatherLongName1 = "blah" 
myClass1.aVariableWithARatherLongName2 = 2.0d 
myClass1.aVariableWithARatherLongName4 = 23105 
myClass1.aVariableWithARatherLongName6 = new Boolean(true) 

MyClass myClass2 = new MyClass() 
myClass2.aVariableWithARatherLongName1 = "other" 
myClass2.aVariableWithARatherLongName2 = 4.0d 
myClass2.aVariableWithARatherLongName4 = 123 
myClass2.aVariableWithARatherLongName6 = new Boolean(false) 


new ObjectBuilder(myClass1).build() 
new ObjectBuilder(myClass2).build() 

nào đã cho tôi kết quả như sau:

new MyClass(aVariableWithARatherLongName1: "blah", aVariableWithARatherLongName2: 2.0d, aVariableWithARatherLongName4: 23105, aVariableWithARatherLongName6: true) 
new MyClass(aVariableWithARatherLongName1: "other", aVariableWithARatherLongName2: 4.0d, aVariableWithARatherLongName4: 123, aVariableWithARatherLongName6: false) 
Các vấn đề liên quan