How to generate a layout from the values โ€‹โ€‹of debug variables in intellij?

Sometimes I need to scoff too long to record POJO in my test cases. I was wondering if in any case I could generate mocks from the data of debug variables 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 in my view of the debug variable, I will 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 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 such functions?

+6
source share
2 answers

I do not know how to do this by default. So something like the groovy code below should work. It takes a little more work to be able to process a list of objects, as you have in your example.

 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(", ")})" } } 

I tested it with the following code (borrowed from your MyClass example:

 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() 

Which gave me the following result:

 new MyClass(aVariableWithARatherLongName1: "blah", aVariableWithARatherLongName2: 2.0d, aVariableWithARatherLongName4: 23105, aVariableWithARatherLongName6: true) new MyClass(aVariableWithARatherLongName1: "other", aVariableWithARatherLongName2: 4.0d, aVariableWithARatherLongName4: 123, aVariableWithARatherLongName6: false) 
0
source

At https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000422864-Generate-Code-from-Object-state-in-Debugger there is a request to add a function, but it seems to be implemented :(

Paired "JTest" software (which looks like an eclipse-based one) from Parasoft seems to be able to generate a test based on the values โ€‹โ€‹of the object being checked: https://docs.parasoft.com/display/JTEST1040/Tracking+ Object + changes + and + Creation + Assertions

Generating dummy data should not be harder, but it does not seem to provide this either. Maybe you can add your own template.

0
source

Source: https://habr.com/ru/post/987414/


All Articles