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.
Verify no (or a set number of) interactions
From the course: Complete Guide To Java Testing with JUnit 5 & Mockito
Verify no (or a set number of) interactions
- [Instructor] When testing interactions between objects, you can verify your code by ensuring a given method is called the expected number of times. Traditional testing approaches often don't have the ability to enforce a constraint like this, but with Mockito, we have a suite of tools that can help us. Mockito offers a method called Times, and it allows you to specify the exact number of times a method should be called. Let's take a look at an example. Here, we have a user service test class. Once fully implemented, it will test whether we can create, update, and delete users. The first test method ensures that when we create a user, we save that user in the data repository. By default, the Verify method verifies the given method was called one time with the stated arguments. In this case, username one, which is Alice. If we use the Times method, we'd write something like this: "times(1)." Here, we verify the user repository is called one time with the given username. It gives us…
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)
-
-