Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialize object for unit testing

Assuming in a unit test I need to have an object all of 50 fields of which are set up with some values. I don't want to manually set up all this fields, as it takes time and annoying...

Somehow I need to get an instance where all the fields are initialized by some not-null values.

and I had an idea - what if I will debug some code, at some point i will get a working instance of this object with some data set up - and I just will serialize it to the disk.

Then I will put this file to test-resources folder, and in unit tests will just deserialize it from that location.

Sounds feasible.. and reasonable? Is there any other idea, or how would you do that?

UPD: I agree serialization is not good in that case. 1) Saved object is not human readable 2) Version could change (highly unlikely) and is not a big problem I believe... So, maybe are there any readable easy-for-serialization formats?

Ideally I would like to have actually source code to be generated. Because it is a java bean, all the getters/setters are there. Why not generate the set of setters calls over the given object in runtime?

like image 637
javagirl Avatar asked Aug 02 '26 17:08

javagirl


1 Answers

This problem has been solved: https://github.com/bbottema/lorem-ipsum-objects

like image 131
HiJon89 Avatar answered Aug 05 '26 07:08

HiJon89



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!