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.

 

36
27 comments

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

0
Avatar
Permanently deleted user

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.

6

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
Avatar
Permanently deleted user

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
Avatar
Permanently deleted user

Desired feature!

0

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.

6
Avatar
Permanently deleted user

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

5

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

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

0
Avatar
Permanently deleted user

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.

8
Avatar
Permanently deleted user

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

0
Avatar
Permanently deleted user

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.

6
Avatar
Permanently deleted user

Any news about it?

1

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

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

4

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

It will be an interesting solve

1

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

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

Would save so much time

0

We still need this. 

0

We defiantly do ! This would make writing unit test far more enjoyable ! 

0

I want that feature too!

0

+ 1 for this feature. 

0

Would be great to have this feature. I just needed and ended up here asking +1

0

Please add this feature, it will save so much time for so many devs!

1

Very helpful! 

0

I'm still want it!

1

Please sign in to leave a comment.