r/javahelp • • 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

7 comments sorted by

View all comments

2

u/DJDavio Dec 13 '20

If you're testing a layer, such as the web or service layer, you could mock the underlying layer and see whether it has been called by your method.