From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Test void methods with Mockito
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Test void methods with Mockito
- One common challenge in testing is how to handle void methods. They don't return a value, but they often have significant side effects. They might change the state of an object, write to a database, log information or trigger other methods. These side effects are often where bugs hide. Let's take a look at an example. Here we have a user notification class that notifies users via email. The notify user method returns void, but it performs a few actions. It logs details and sends the email, all of which return void. With this method, we'll want to test the case where the email is sent successfully, as well as the case where the email is not sent and an exception is thrown. In our test class, there are a few things set up for us. We have our mocking ready to go as well as two test methods. Let's implement these. The primary way to test void methods is to verify they're called with the expected arguments. We'll call our notify user method in the first test and then verify the…
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Spy on real objects with Mockito4m 31s
-
(Locked)
Test exception handling with Mockito2m 10s
-
(Locked)
Test void methods with Mockito5m 10s
-
(Locked)
Verify no (or a set number of) interactions5m 1s
-
(Locked)
GetAllValues with ArgumentCaptor5m 1s
-
(Locked)
Test asynchronous code with Mockito3m 17s
-
(Locked)
Mock a static method with Mockito3m 15s
-
(Locked)
Challenge: Test an ordering service1m 2s
-
(Locked)
Solution: Test an ordering service8m 21s
-
(Locked)
-
-