Generate Code from Object state in Debugger

Answered

Is there such a feature or plugin?

Story:

While looking at a variable in the debugger, I would like to copy code that generates this object. The Code recursively re-creates all the Objects. This would be useful to create a unit-test out of productive data.

Limitiations:

This would only work with simple beans with matching constructors. A great extra would be to support Lombok Builders.

 

14 comments
Comment actions Permalink

Why debugger? Can you use Mockito or something like that?

0
Comment actions Permalink

It just happens sometimes that I can reproduce a bug in the debugger, and then want to re-create this data structure in a unit test (to fix the bug). In a complex application this might be difficult without the proper input values.

3
Comment actions Permalink

It is usually very hard to recreate an object, debugger could probably save the object state somewhere to help user with this, true.

something like this: https://youtrack.jetbrains.com/issue/IDEA-153105

0
Comment actions Permalink

Yeah, something like https://youtrack.jetbrains.com/issue/IDEA-134919, but not at runtime.

In my case the objects are very simple JavaBeans. But sure in general it is very difficult to recreate them.

0
Comment actions Permalink

Desired feature!

0
Comment actions Permalink

This would be very helpful when creating test cases.

Example test case

assertNotEquals(Collections.emptyMap(), errorLogger.getErrorList());

Example debugger state:

Now, I quickly want to generate a Map containing these entries.

 

It would be very cool if IntelliJ could generate Lists and Maps. Skeletons would be enough. No need to support real business objects.

5
Comment actions Permalink

Yes this would make writing unit tests very simple and fast!

4
Comment actions Permalink

For this partucular case you do not need to create an exact collection object, you can copy toString value from the debugger and compare with it in your test.

Creating a collection is not hard, but only for primitive containing objects (dumping toString for them works even better in my opinion).

0
Comment actions Permalink

It would be a fantastic feature especially for unit tests. Look forwarding!

0
Comment actions Permalink

I just added this idea to my TODO list and found this thread)
Sometimes it makes sense to convert descriptive JSON file into complex java object for unit tests. I employed this idea and use it a lot.  You can capture the state of an object at runtime, convert it to JSON string using Gson for instance in the debugger and set up such a JSON file for tests. Not only it's readable, but it makes writing tests really fast.

6
Comment actions Permalink

I also stumbled upon this problem today. Having a quite complicated domain, such plugin would save me hours of work preparing mocks :)

0
Comment actions Permalink

Right clicking on an object in variables or memory and selecting "Copy as java code stub" or "Generate mock from value" would be great options.

It's probably tricky to do recursively and instantiate all variables in the right way, including builders etc. but a stub with a bunch of setters or assignments would be enough for me. It's not a problem if the generated code is not perfect, as long as the object setters match the values in the debugger it's a great time saver.

4
Comment actions Permalink

Any news about it?

0
Comment actions Permalink

If yo mean the request about possibility to recreate the object in debugger - there are no such plans. But there is a related request you can vote and follow: https://youtrack.jetbrains.com/issue/IDEA-134919

0

Please sign in to leave a comment.