r/learnjava • • Aug 25 '20

What are some good resources to learn testing with JUnit and Mockito?

I would like to learn more about unit testing, theory and best practices with some practical examples on JUnit and Mockito. I've found some tutorials in the internet, but they don't usually dive too deep. Is there any books or online course I could use as a reference and to learn the basics but with some fundamentals?

Thank's!

48 Upvotes

8 comments sorted by

0

u/fustup Aug 26 '20

I think the best way to write a test and learn about Mockito is to "just do it". Sounds stupid, but let me explain:

You should have a clear intention of what exactly your test is: precondition, execution, assertion.

You should write the test as simple and "as prosa-like" as you can do.

And after that you"wire it up", write the mocks you need, the methods, the what have you.

As for best practices: if you do more than just basic mocking be wary. Argument Captors are awesome, maybe the odd method stub returning the only parameter as a result. But that might already be a code smell.

How to write good mocks? Don't 😁(do, of course, but within strict limits)

1

u/gunterspace Aug 26 '20

Thank you for the tip! In the end, the best way to do something is doing it 😋 I'll take that advice and start writing more tests, even for personal projects. Also, reading other people unit tests will help as well to get to know how it's done.

1

u/Xermal Aug 25 '20

RemindMe!

2

u/RemindMeBot Aug 26 '20 edited Aug 26 '20

Defaulted to one day.

I will be messaging you on 2020-08-26 23:43:43 UTC to remind you of this link

2 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback

0

u/DeepImpact95 Aug 26 '20

Remind Me!

0

u/[deleted] Aug 26 '20

RemindMe!

14

u/[deleted] Aug 26 '20

[removed] — view removed comment

1

u/gunterspace Aug 26 '20

Thank you! I will give it a try, seems a good starting point.