Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 866
Jaap Oudejans Avatar asked May 18 '15 11:05

Jaap Oudejans


People also ask

How do I show debug variables in IntelliJ?

You can click on the Run icon in the gutter area and select the Debug option. You can invoke context actions on the class or main method by using Alt+Enter and choose the Debug action. You can also start it from the Run menu, or by pressing Shift F9.

How do I change the value of a variable in debug mode IntelliJ?

Select a variable or a property of a complex object in the Debug window, press F2 or right-click and choose Set Value... from the context menu, and then specify a new value and press Enter .

How do I view a variable in IntelliJ?

Right-click at the end of the desired line and choose Add Inline Watch in the context menu. In the input field that appears, specify an expression to watch. Right-click a variable or expression that you want to watch and choose Add Inline Watch in the context menu.

How do I debug test cases in IntelliJ?

Debug failed tests If you don't know why a test fails, you can debug it. In the editor, click the gutter on the line where you want to set a breakpoint. There are different types of breakpoints that you can use depending on where you want to suspend the program. For more information, refer to Breakpoints.


Video Answer


1 Answers

There is a feature request on https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000422864-Generate-Code-from-Object-state-in-Debugger But it doesn't seem to be implemented :(

The paying software "JTest" (that looks like being eclipse based) from Parasoft seems to do be able to generate the test based on the values of an inspected objected: https://docs.parasoft.com/display/JTEST1040/Tracking+Object+Changes+and+Creating+Assertions

Generating the mock data shouldn't be any harder but doesn't seem to provide it either. Maybe you can add your own template.

like image 88
Jerome Hordies Avatar answered Oct 07 '22 23:10

Jerome Hordies