Mockito return based on the input parameter

Mockito return based on the input parameter

Mockito, the defacto mocking library in the Java world that is widely adopted. It is used one way or another by almost any Java project. Mockito is known for its versatility which offers powerful features for dynamic testing scenarios. In this article, we discuss how to return a result of a mock object based on the input parameter.

Mockito is the defacto mocking library in the Java world. It’s used one way or another by almost any Java project. In this article, we discuss how to return a result of a mock object based on the input parameter.

What we want to essentially achieve is to instruct Mockito to return a result of a method based on the passed input parameter. For example, we have a method under test as follows:

public Optional<String> retrieve(String key) {
        return Optional.ofNullable(cache.get(key));
}

We would like to test the behavior of the retrieve method in two cases:

  • When the cache has an entry for a key
  • When the cache does not have an entry for a key

For that, we can mock the cache object and based on the input parameter return a result: a random value or null.

To achieve that we need to capture the input parameter to the cache.get() method and act upon it accordingly.

@ParameterizedTest
@MethodSource("cacheTestCases")
void testRetrieve(String key, boolean expected) {
    when(cache.get(any())).thenAnswer((Answer<String>) invocation -> StringUtils.isBlank(invocation.getArgument(0)) ? null : UUID.randomUUID().toString());

    assertEquals(expected, propertyParameter.retrieve(key).isPresent());
}

Let’s go through the code. First, we mocked the cache class.

Then we instructed Mockito to answer the get() method invocation based on the input parameter, here is the cache key. If the input parameter is empty or null, Mockito returns null. Otherwise a random UUID.

Finally, in the last line of the test, we inspect the result of the retrieve method. Below is the full example,

@ExtendWith(MockitoExtension.class)
public class PropertyParameterTest {

    @InjectMocks
    private PropertyParameter propertyParameter;

    @Mock
    private PropertyParameter.Cache cache;

    @ParameterizedTest
    @MethodSource("cacheTestCases")
    void testRetrieve(String key, boolean expected) {
        when(cache.get(any())).thenAnswer((Answer<String>) invocation -> StringUtils.isBlank(invocation.getArgument(0)) ? null : UUID.randomUUID().toString());

        assertEquals(expected, propertyParameter.retrieve(key).isPresent());
    }

    private static Stream<Arguments> cacheTestCases() {
        return Stream.of(arguments("", false), arguments(null, false), arguments("random", true));
    }
}

Conclusion

In this article, we discuss how in Mockito we can return a method result based on the input parameter. For that, we use thenAnswer that allows capturing the input parameter which then based on value, can decide what the invocation result should be. As always, the tutorial’s code is available on Geeky Hacker GitHub repository.

Inline/featured images credits