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.

 

20 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.

5
Comment actions Permalink

Any news about it?

1
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
Comment actions Permalink

I want to revive this because this feature will save hours of work for a lot of developers.

4
Comment actions Permalink

I wonder, if this feature would have been there already.

It will be an interesting solve

1
Comment actions Permalink

Touseefrafique

The corresponding internal task is still in progress.

Once the feature is implemented, there will be an update in IDEA-134919, so you can follow it to stay posted on the progress.

0
Comment actions Permalink

Hi guys, same here, my use case is this:I have a bug and I find that is caused by an object with a combination of set members that cause problems 

I want to illustrate the bug in a unit test with the object like a mook or a prototype or just a new object with setters 

I think that having an option like (copy state) and will generate in the clipboard the code to recreate the exact state of that object at that moment of time  then just ctrl+v in your unit test to make it fail

it will be a great feature and for sure will be unique on market, I can`t remember seeing this on other editors

0
Comment actions Permalink

Would save so much time

0
Comment actions Permalink

We still need this. 

0

Please sign in to leave a comment.