Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I integrate Hamcrest matchers with Mockito in a JUnit test


How do I integrate Hamcrest matchers with Mockito in a JUnit test


Integrating Hamcrest matchers with Mockito in a JUnit test allows you to write more expressive and flexible unit tests. Here's a step-by-step guide on how to do it:

Step 1: Add Dependencies

First, you need to add the necessary dependencies to your project. If you're using Maven, add the following to your `pom.xml`:

xml

    org.hamcrest
    hamcrest-library
    2.2
    test



    org.mockito
    mockito-core
    {mockitoversion}
    test



    org.mockito
    mockito-junit-jupiter
    {mockitoversion}
    test

Replace `{mockitoversion}` with the latest version available from Maven Repository.

Step 2: Import Necessary Classes

In your test class, import the necessary classes:

java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;

Step 3: Use Mockito to Mock Objects

Use Mockito to create mock objects for dependencies in your class under test:

java
@ExtendWith(MockitoExtension.class)
public class MyTest {

    @Mock
    private MyDependency dependency;

    @InjectMocks
    private MyClass myClass;

    @Test
    void testMyMethod() {
        // Mock behavior
        when(dependency.doSomething()).thenReturn("Mocked result");
        
        // Call the method under test
        String result = myClass.myMethod();
        
        // Use Hamcrest to assert the result
        assertThat(result, equalTo("Mocked result"));
    }
}

Step 4: Use Hamcrest Matchers for Assertions

Hamcrest provides a variety of matchers to make your assertions more readable and flexible. Here are a few examples:

- Equality: `assertThat(result, equalTo("Expected value"));`
- String Matching: `assertThat(result, containsString("Substring"));`
- Collection Contents: `assertThat(myList, hasItem("Item"));`
- Null Checks: `assertThat(myObject, notNullValue());`

Step 5: Combine with Mockito's `argThat`

If you need to verify that a method was called with a specific argument, you can use Mockito's `argThat` method along with a Hamcrest matcher:

java
verify(dependency).doSomething(argThat(equalTo("Expected argument")));

This approach allows you to leverage the expressive power of Hamcrest matchers within Mockito's mocking framework.

Example Use Case

Here's a complete example that integrates Hamcrest with Mockito in a JUnit test:

java
@ExtendWith(MockitoExtension.class)
public class MyServiceTest {

    @Mock
    private MyRepository repository;

    @InjectMocks
    private MyService service;

    @Test
    void testGetUser() {
        // Mock behavior
        User user = new User("John Doe", "
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy71675 = 'j&#111;hn' + '&#64;';
 addy71675 = addy71675 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy71675 + suffix + '\'' + attribs + '>' );
 document.write( addy71675 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>");
        when(repository.getUser(anyString())).thenReturn(user);

        // Call the method under test
        User result = service.getUser("
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy59147 = 'j&#111;hn' + '&#64;';
 addy59147 = addy59147 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy59147 + suffix + '\'' + attribs + '>' );
 document.write( addy59147 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>");

        // Use Hamcrest to assert the result
        assertThat(result.getName(), equalTo("John Doe"));
        assertThat(result.getEmail(), equalTo("
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy35243 = 'j&#111;hn' + '&#64;';
 addy35243 = addy35243 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy35243 + suffix + '\'' + attribs + '>' );
 document.write( addy35243 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>"));
    }
}

In this example, Mockito is used to mock the `MyRepository` dependency, and Hamcrest matchers are used to assert that the returned `User` object has the expected properties.

Citations:
[1] https://www.infoworld.com/article/2257253/junit-5-tutorial-part-1-unit-testing-with-junit-5-mockito-and-hamcrest.html
[2] https://www.vogella.com/tutorials/Mockito/article.html
[3] https://www.javacodegeeks.com/2015/11/hamcrest-matchers-tutorial.html
[4] https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.mockito.internal.hamcrest.HamcrestArgumentMatcher
[5] https://www.baeldung.com/java-junit-hamcrest-guide
[6] https://stackoverflow.com/questions/33351500/mockito-jmockit-hamcrest-matchers-how-to-verify-lists-collections
[7] https://stackoverflow.com/questions/45218799/how-to-make-junit4-hamcrest-1-3-mockito-work-from-eclipse-and-tycho
[8] https://www.youtube.com/watch?v=ZsWgjdAtfKc
[9] https://site.mockito.org/javadoc/current/org/mockito/hamcrest/MockitoHamcrest.html
[10] https://hamcrest.org/JavaHamcrest/tutorial
[11] https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.mockito.hamcrest.MockitoHamcrest