Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock static class seems records wrong invocations if called nested method throws exception #2616

Closed
mariohofmann opened this issue Apr 10, 2022 · 0 comments

Comments

@mariohofmann
Copy link

If mocking a static class where method calls another static method within same class and inner class is throwing an expception the
the stubbing does not work as expected (instead of returning stubbed answer the execption is thrown)

Class StaticTest.java:

import java.util.concurrent.atomic.AtomicInteger;

public class StaticClass {
    public static final AtomicInteger countOuter = new AtomicInteger(0);
    public static final AtomicInteger countInner = new AtomicInteger(0);
    public static final AtomicInteger resultInner = new AtomicInteger();

    public static int outerMethod(String aParam1, Integer aParam2, Object aParam3) {
        countOuter.incrementAndGet();
        resultInner.set(innerMethod(aParam1, aParam2));
        return countOuter.incrementAndGet();
    }

    public static int innerMethod(String aParam1, Integer aParam2) {
        throw new NullPointerException();
//        return countInner.incrementAndGet();
    }
}

Class TestStaticMock.java:

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Answers.CALLS_REAL_METHODS;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

public class TestStaticMock {
    @Test
    void testMock() {
        try (MockedStatic<StaticClass> mocked = mockStatic(StaticClass.class, CALLS_REAL_METHODS)) {
            mocked.when(() -> StaticClass.outerMethod(any(), any(), any())).thenReturn(Integer.valueOf(-1));

            assertEquals(-1, StaticClass.outerMethod("DUMMY", Integer.valueOf(10), Double.valueOf(2.0))); // (1)
//            assertThrows(NullPointerException.class, () -> StaticClass.outerMethod("DUMMY", Integer.valueOf(10), Double.valueOf(2.0)));  // (2)

            mocked.verify(() -> StaticClass.outerMethod("DUMMY", Integer.valueOf(10), Double.valueOf(2.0)));
            mocked.verifyNoMoreInteractions();
        }
    }
}

Expcected behaviour is assertEquals succeeds because the stubbing returns the expected value but exception raised
by innerMethod is returned. If line marked with (1) is comment out and comment for line with (2) is removed the validation
of check for noMoreInteraction prints

org.mockito.exceptions.verification.NoInteractionsWanted: 
No interactions wanted here:
-> at TestStaticMock.testMock(TestStaticMock.java:20)
But found this interaction on mock 'StaticClass.class':
-> at TestStaticMock.lambda$0(TestStaticMock.java:14)
***
For your reference, here is the list of all invocations ([?] - means unverified).
1. [?]-> at TestStaticMock.lambda$0(TestStaticMock.java:14)
2. -> at TestStaticMock.lambda$1(TestStaticMock.java:17)
3. [?]-> at StaticClass.outerMethod(StaticClass.java:10)

	at TestStaticMock.testMock(TestStaticMock.java:20)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
	at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
...

showing inner invocation not expected and recording of stubbing.

fishautumn added a commit to fishautumn/mockito that referenced this issue Jun 14, 2022
fishautumn added a commit to fishautumn/mockito that referenced this issue Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant