Table of contents
1.
Introduction
2.
Custom Argument Matcher
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Custom Matchers

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Mockito is an open-source framework used for testing in Java. The Mockito framework allows the creation of automated unit tests and mock tests for behavior-driven development(BDD) or test-driven development(TDD).

Argument Matchers are primarily used for stubbing and performance flexible verification in Mockito. We can access the Argument Matcher class using the org.mockito package. 

To access the org.mockito package, we need to add the following dependencies,

<dependency>
    <groupId>org.mockito</groupId> 
    <artifactId>mockito-core</artifactId>
    <version>2.21.0</version> 
    <scope>test</scope>
</dependency>
You can also try this code with Online Java Compiler
Run Code

 

Let us look at an example of Argument Matchers,

when(codingNinjas.analyze(anyString())).thenReturn("Hey Ninja");
You can also try this code with Online Java Compiler
Run Code

In the above example, we return “Hey Ninja” whenever any string is passed to the compiler.

Custom Argument Matcher

Sometimes the regular Argument Matchers are not enough, and the developer needs custom-created matchers for the best possible approach with the highest quality tests. Custom Argument Matchers can prove to be a more clean and maintainable approach for testing. 

Let us look at an example of Custom Argument Matchers,

public class customMatcher implements ArgumentMatcher<Message> {
    private Message left;
    @Override
    public boolean matche(Message right) {
        return left.getFrom().equals(right.getFrom()) &&
          left.getText().equals(right.getText()) &&
          left.getTo().equals(right.getTo()) &&
          right.getId() != null;
          right.getDate() != null &&
    }
}
You can also try this code with Online Java Compiler
Run Code

 

The test for the above Java code will look something like this,

verify(messageService, times(1)).deliverMessage(argThat(new customMatcher(message)));
You can also try this code with Online Java Compiler
Run Code

FAQs

  1. What is Dynamic Testing?
    A Dynamic Test is one of the most commonly used tests, and it is generated at the run time. We use the TestFactory method to generate Dynamic Tests in JUnit5. TestFactory method is a non-static and non-private method.
  2. What are testing libraries?
    The testing library is a collection of packages that helps the developer to test the UI components in a user-centric way.
  3. What is JUnit?
    JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.
  4. What is Mockito?
    Mockito is an open-source framework used for testing in Java. The Mockito framework allows the creation of automated unit tests and mock tests for behavior-driven development(BDD) or test-driven development(TDD).
  5. What is Java?
    Java is an object-oriented, class-based, high-level programming language. It is based on the concept of WORA (write once use anywhere) for developer ease.

Key Takeaways

This Blog covered all the necessary points about the Custom Matchers or Custom Argument Matchers in Mockito. We further looked at some code snippets to understand Custom Matchers.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass