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

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