r/javahelp • u/nasty_haze • Dec 12 '20
Question regarding best practices when unit testing void methods
Hey gang,
So I have been brushing up on my TDD practices with JUnit5. I have noticed that when testing void methods, namely ones that insert data into a structure, I am testing the success of the insertion with toString() or search() methods.
Should I be coupling my unit tests like this?
What would be an alternative to better expose the functionality within the void method?
Or is this type of testing, much like with private methods, simply how it is done?
Thanks in advance for your insights!
17
Upvotes
2
u/kakipipi23 Dec 13 '20
Sometimes the functionality can be decoupled in such way that one method returns the changes that needs to be done and another one is just updating your data structure / db in a general manner. If it fits your use case go ahead and do that.
Ill give an example: lets say you have a business logic that receives an account, updates the balance according to some independent logic and saves it. You can have one method that does all that, or you can have an update method that calculates the balance update and then another one to save an account.
Hope I was clear :)