lundi 18 mai 2015

How to generate a mock from debug variable values in intellij?

Sometimes I need to mock rather long, to write out, POJOs in my test cases. I was wondering if there is anyway I could generate the mocks from debug variable data in Intellij (14)?

As an example, we have a class:

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
}

And within my debug variable view I would have a list of MyClass variables:

- myClasses = {ArrayList@0} size = 5
    - 0 = {MyClass@1}
        - aVariableWithARatherLongName1 = {String} "value 1"
        - aVariableWithARatherLongName2 = {Double} 2.0
        - aVariableWithARatherLongName3 = {String} "value 1"
        ...
    - 1 = {MyClass@2}
        - aVariableWithARatherLongName1 = {String} "value 2"
        - aVariableWithARatherLongName2 = {Double} 2.0
        - aVariableWithARatherLongName3 = {String} "value 2"
        ...
    + 2 = {MyClass@3}
    + 3 = {MyClass@4}
    + 4 = {MyClass@5}

And then the plugin or Intellij would generate something like below based on the selected language (Groovy in this example):

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', ...),
    ]
}

Is something like this possible with Intellij (14) or are there any plugins that provide functionality like this?

Aucun commentaire:

Enregistrer un commentaire