How to verify that a specific method was not called using Mockito?

Cover Image for How to verify that a specific method was not called using Mockito?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

šŸ“ Title: How to Verify that a Specific Method was not Called using Mockito

šŸ‘‹ Hey there, tech enthusiasts! Welcome back to our blog. Today, we have an interesting topic to discuss - how to verify that a specific method was not called using Mockito. šŸŽ‰

So, you're working on a project and you've stumbled upon a scenario where you need to make sure that a particular method is not called on an object's dependency. Let's dive into the details and understand how to tackle this situation effortlessly. šŸ’Ŗ

šŸ”Ž Understanding the Problem:

To understand the problem better, let's consider a sample implementation. We have an interface called Dependency with a method named someMethod(). We also have a class called Foo, which accepts an instance of Dependency as a parameter in its method bar(). Our goal is to verify that the someMethod() of Dependency is not called when executing the bar() method of Foo.

šŸ“ Solution:

To achieve this verification, we can utilize the power of Mockito, a popular testing framework that provides a simple and elegant way to mock objects and verify their behavior. Mockito offers a variety of methods to verify method calls, but we need one that verifies the absence of a specific method call. Let's see how it's done:

  1. Mock the Dependency:

First, we need to create a mock instance of the Dependency interface. In our test, we can achieve this by using the mock() static method provided by the Mockito framework. This will create a mock object of the Dependency interface, allowing us to control its behavior and verify method calls.

final Dependency dependency = mock(Dependency.class);
  1. Invoke the Method under Test:

Next, we need to execute the method that we want to test. In our case, this is the bar() method of the Foo class, passing the mocked Dependency object as a parameter.

foo.bar(dependency);
  1. Verify the Absence of Method Call:

Finally, we can use the verifyNoInteractions() method of Mockito to ensure that the someMethod() of the Dependency object was not called during the execution of our test. This method verifies that there were no interactions with the specified mock object.

verifyNoInteractions(dependency);

And that's it! You have successfully verified that the someMethod() of the Dependency object was not called. šŸŽ‰

šŸ“ Example Test:

To give you a complete picture of how this verification works, let's take a look at an example test using the sample code provided earlier.

public class FooTest {
    @Test
    public void dependencyIsNotCalled() {
        // Arrange
        final Foo foo = new Foo(...);
        final Dependency dependency = mock(Dependency.class);
        
        // Act
        foo.bar(dependency);
        
        // Assert
        verifyNoInteractions(dependency);
    }
}

In this test, we create an instance of Foo and a mock Dependency object. We then invoke the bar() method of Foo, passing the mock Dependency object. Finally, we verify that there were no interactions with the Dependency mock using verifyNoInteractions(dependency).

šŸ“£ Call to Action:

Congratulations! You've learned a neat trick to verify that a specific method was not called using Mockito. This knowledge will come handy in your future tests, ensuring the correctness of your code.

Now, it's your turn to put this technique into practice. Try it out in your own tests and let us know your experience. Did it help you uncover unexpected interactions? Share your feedback and thoughts in the comments section below. We'd love to hear from you! šŸ¤—

Remember, sharing is caring! If you found this article helpful, don't forget to hit the share button and spread the word. Until next time, happy testing! āœŒļø


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my

Matheus Mello
Matheus Mello