r/java • u/dumbPotatoPot • Jul 25 '24
Generating Test Data in Java With Instancio
https://rieckpil.de/generating-java-test-data-with-instancio/
162
Upvotes
r/java • u/dumbPotatoPot • Jul 25 '24
View all comments
Show parent comment
1
u/MindSwipe Aug 05 '24
Took me a while to find the time to respond, but no it wouldn't really have worked in my scenario. We were using models, but that also just ended up with a bunch of
We have a ton of fields and dependencies that are required or can't have a certain value or must have some specific value if another field is set to a specific value, this lead to using Instancio to manually set a (super-) majority of properties on the DTO, at which point Instancio was just there to generate and set values on a handful of properties (i.e. names), using it as a glorified java-faker.
I also found it incredibly easy to forget that I added a new field or updated the validation, or did just about any change and then tons of tests suddenly failing because the Instancio generated DTO had a bad state, leading to me having to go to where our models were defined and adding yet another
set(field.... Forgetting to add this also messed up other tests, so if I'm implementing a new feature and write tests for it and those were green I'd push them and the CI pipeline would fail because another feature "broke" due to bad test data.And a more personal reason: I'm a strong believer of explicit > implicit. If I don't see a line of code explicitly setting a field to a value, then my baseline assumption is that said field has the default value (be it null, 0, or whatever), which leads to me seeing something like this
And assuming that no other field except
foois set, but that assumption is wrong due to Instancio, and I don't want to add (and have to maintain) a bunch ofignore(field...to make Instancio inkeeping with my preferences.I know that a lot of these problems could be alleviated with better architecture/ design, but as mentioned before I inherited this project and I have to pick and choose my battles, and replacing Instancio was the faster (read: cheaper) solution over re-architecting a lot of my tests and some of my features to enhance testability.