[Java] Pass arguments to constructor in Mockito / Set method default call to callRealMethod

A little note from Mockito

Pass arguments to the constructor


package jp.jig.product.live.server.common.service;

import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ApiServiceTest {

    public static class ApiService {

        private final String endpoint;
        private final int timeout;

        public ApiService(String endpoint, int timeout) {
            this.endpoint = endpoint;
            this.timeout = timeout;
        }

        public String request() {
            if (endpoint == null) {
                throw new IllegalStateException("endpoint is null");
            }

            return endpoint;
        }

        public boolean validTimeout() {
            return timeout > 0;
        }
    }

    @Test
    public void test() {

        ApiService apiService = mock(ApiService.class);
        when(apiService.request()).thenCallRealMethod();
        apiService.request(); // -> throw IllegalStateException
    }

}

For example, when creating a mock of ʻApiServiceas shown above and calling therequest ()` method. I think there are situations where you want to set a value in the constructor.

At this time, you can pass the argument to the constructor while creating a mock object by using MockSetting # useConstructor as the second argument ofmock ().


    @Test
    public void test() {
        ApiService apiService = mock(ApiService.class, withSettings().useConstructor("http://localhost", 100));
        when(apiService.request()).thenCallRealMethod();
        apiService.request(); // -> http://localhost
    }

If you try to put logging in the constructor, the log will also be output, so you can see that the processing in the constructor is also being executed.

Make the default call callRealMethod

I basically want to call the original method when testing, but I want to mock only some of the methods. .. .. I think there is something like that.

ApiService apiService = mock(ApiService.class,
 withSettings().defaultAnswer(CALLS_REAL_METHODS));

This defaults to callRealMethod by passing CALLS_REAL_METHODS to MockSetting # defaultAnswer.

However, there is one caveat, you need to be careful when using when () in a method that is callRealMethod.

    @Test
    public void test() {
        ApiService apiService = mock(ApiService.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
        when(apiService.request()).thenReturn("hogehoge"); // -> throw IllegalStateException
    }

The above code seems to work normally, but the method is called at the time of when (apiService.request ()). It seems better to use doReturn to avoid this.


    @Test
    public void test() {
        ApiService apiService = mock(ApiService.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
        doReturn("hogehoge").when(apiService).request();

        apiService.request(); // -> hogehoge
    }

You can use doReturn to set the mock without calling callRealMethod.

Recommended Posts

[Java] Pass arguments to constructor in Mockito / Set method default call to callRealMethod
Summary of how to implement default arguments in Java
Call the super method in Java
Call Java method from JavaScript executed in Java
[Android] Call Kotlin's default argument method from Java
Java method call from RPG (method call in own class)
Check method call arguments in blocks with RSpec
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
[Java] Implementation method memo to set WS-Security Username Token in SOAP Stub of axis2
A study method for inexperienced people to pass Java SE 8 Silver in one month
How to call functions in bulk with Java reflection
[Java] How to omit the private constructor in Lombok
How to mock a super method call in PowerMock
JAVA constructor call processing
How to get the class name / method name running in Java
How to pass an object to Mapper in MyBatis without arguments
Easy way to check method / field list in Java REPL
[Android, Java] Convenient method to calculate the difference in days
Create a method to return the tax rate in Java
How to call and use API in Java (Spring Boot)
[Kotlin] Get Java Constructor / Method from KFunction and call it
How to change arguments in [Java] method (for those who are confused by passing by value, passing by reference, passing by reference)
How to set Java constants
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Sample code to call the Yahoo! Local Search API in Java