Skip to content
Advertisement

Setter in DTO just for integration testing?

I couldn’t find the info I’m looking for hence posting here for suggestion and getting to know better approach.

I have an immutable DTO object like:

JavaScript

Now I’m working on an integration test with testcontainer where I want the accountOpeningDate and installmentPaidDate to be dynamic value hence in order to set these values from Integration Tests, is it a good idea to add only 2 setters to this DTO which is only to be used for Integration Tests like below?

JavaScript

Or is there a better approach than this? Looking for recommendations.

TA

Advertisement

Answer

It is not acceptable you will lose the immutability of the object. With immutable classes, we can’t modify the state but must create a new instance with the mutated state.
The builder pattern is very useful in this case.
Example:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement