Generate Code from Object state in Debugger


Is there such a feature or plugin?


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.


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



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


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.


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:


Yeah, something like, but not at runtime.

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


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.


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


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


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


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.


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


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.


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:


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


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

It will be an interesting solve



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.


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


Would save so much time


We still need this. 


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


I want that feature too!


+ 1 for this feature. 


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


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