1

I have a class that is only for json serialization with field access using Jackson , now I have a method that takes some inputs and create an instance of that class with properties populated using setters. To unit test this method, I need to compare every property of that class. Without public getters, it can only be done via reflection which I am trying to avoid. Also I don't want to override equals method on that class using all properties.

If I add getters, then I am changing the api of that class just for unit testing. What is the best solution in this case and why? Any thought on this will be appreciated. Thanks.

Jay Huang
  • 119
  • 2
  • 11

1 Answers1

0

Or, you can test the form of the class as it would be normally used - its serialized version. Since serializator is a component which you can safely assume works, simply serialize object after tested class does its work and compare result with reference JSON. This will indirectly test that your class sets properties correctly.

On the other hand, one might argue how deeply impacting is adding getters. If this is the kind of autogenerated boilerplate code, maybe it's better to reconfigure generator to include getters. If not, and you deliberately decided not to implement getters, it probably should stay this way.

Community
  • 1
  • 1
k.m
  • 30,794
  • 10
  • 62
  • 86