Codementor Events

How to Fix UnfinishedStubbingException In Java

Published Nov 17, 2024
How to Fix  UnfinishedStubbingException  In Java

1. Introduction

UnfinishedStubbingException is a common Mockito error. In this quick tutorial, you will learn the usual causes of this exception and how to fix it.

2. What Mockito Says about UnfinishedStubbingException

As its name suggests, this exception occurs whenever you have an incomplete stubbing configuration. Typically, this can happen in any of the following situations:

  • The thenReturn() is missing
  • You are trying to stub a final method, which is not supported (Before Mockito 2.x)
  • You are stubbing the behavior of another mock inside before thenReturn instruction is completed

3. How to Reproduce

We will use the code of the Calculator class that was used in this previous tutorial on InvalidUseOfMatchersException.

3.1. Missing thenReturn()

The following test case will throw an UnfinishedStubbingException :

    @Test
    public void testUnfinishedStubbing_missingThenReturn() {
        //Given
        String expectedResult = "1 + 1 = 2";
        when(formatter.format(eq('+'),anyDouble(),anyDouble(),anyDouble()));
        //When
        String result = calculator.calculate(1,1,'+');
        //Then
        assertEquals(expectedResult,result);
    }

Running this test case will produce the following output:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.kloudly.exception.CalculatorTest.testUnfinishedStubbing_missingThenReturn(CalculatorTest.java:40).

This exception happens here because of this line : when(formatter.format(eq('+'),anyDouble(),anyDouble(),anyDouble()));

The mock object formatter is not configured properly as the thenReturn() is missing.

How to Fix
Complete the stubbing by adding the thenReturn() as shown below:

    @Test
    public void testUnfinishedStubbing_missingThenReturn() {
        //Given
        String expectedResult = "1 + 1 = 2";
        when(formatter.format(eq('+'),anyDouble(),anyDouble(),anyDouble())).thenReturn(expectedResult);
        //When
        String result = calculator.calculate(1,1,'+');
        //Then
        assertEquals(expectedResult,result);
    }

3.2. Stubbing a method while another Stubbing is Pending

The test case below will also throw an UnfinishedStubbingException:

    @Test
    public void testUnfinishedStubbing_beforeThenReturnIsComplete() {
        //Given
        when(formatter.format(eq('+'),anyDouble(),anyDouble(),anyDouble())).thenReturn(formatter.getDefaultResult());
        //When
        String result = calculator.calculate(1,1,'+');
        //Then
        assertEquals(expectedResult,result);
    }

The problem here is that we are calling another method getDefaultResult() on the mocked object formatter before the thenReturn() is complete.

More generally, this will happen if you try to stub a method while stubbing of another method is still in progress.

How to fix
Ensure you don't call any other method of a mocked object before the thenReturn() completes.

    @Test
    public void testUnfinishedStubbing_beforeThenReturnIsComplete() {
        //Given
        String expectedResult = formatter.getDefaultResult();
        when(formatter.format(eq('+'),anyDouble(),anyDouble(),anyDouble())).thenReturn(expectedResult);
        //When
        String result = calculator.calculate(1,1,'+');
        //Then
        assertEquals(expectedResult,result);
    }

We have moved the call to formatter.getDefaultResult() before the line containing the thenReturn().

4. Conclusion

In this brief tutorial, you learned about the UnfinishedStubbingException and how to fix it.
You can find the complete code of this article here in GitHub

_This article was originally published at: https://nkamphoa.com

Discover and read more posts from Noel KAMPHOA
get started