Mockito thenThrow throws mockito exception

I am trying to mock restOperation to throw exceptions, but I am getting MockitoException instead of correct exception. This is strange to me because when I try to check the happy path, almost the same mock invoke works fine. What's wrong with this:

Working layout:

Mockito.when(restOperations.exchange(
            Mockito.anyString(), Mockito.<HttpMethod>any(), Mockito.<HttpEntity<?>>any(),
            Mockito.<Class<UserByRoleHolder>>any())).thenReturn(new ResponseEntity<>(userByRoleHolder, HttpStatus.OK));

      

Mock throwing MockitoException:

Mockito.when(restOperations.exchange(
            Mockito.anyString(), Mockito.<HttpMethod>any(), Mockito.<HttpEntity<?>>any(),
            Mockito.<Class<UserByRoleHolder>>any())).thenThrow(new ConnectException("Test exception"));

      

Here is my implementation of the method:

ResponseEntity<UserByRoleHolder> entity = null;
try {
    entity = restOperations.exchange(userProfileUrl, HttpMethod.GET,
                authorizedHttpEntityFactory.getAuthorizedHttpEntity(null), UserByRoleHolder.class);
    } catch (RestClientException e) {
        throw new ConnectException(e.getMessage());
    }
    return entity.getBody();

      

Stack trace:

Unexpected exception, expected<java.net.ConnectException> but was<org.mockito.exceptions.base.MockitoException>
java.lang.Exception: Unexpected exception, expected<java.net.ConnectException> but was<org.mockito.exceptions.base.MockitoException>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
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:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
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:497)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: java.net.ConnectException: Test exception
at com.stanleyblackanddecker.toolboxcore.gateway.AuthGatewayTest.testGetAllUsersNoAuthPresent(AuthGatewayTest.java:215)
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:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 37 more

      

+3


source to share


1 answer


Throws: org.mockito.exceptions.base.MockitoException: The fixed exception is not valid for this method! Invalid: java.net.ConnectException: validation exception

As pointed out in the exception message (and said in the comments), you should throw RestClientException

, not ConnectException

:



Mockito.when(restOperations.exchange(
        Mockito.anyString(), Mockito.<HttpMethod>any(), Mockito.<HttpEntity<?>>any(),
        Mockito.<Class<UserByRoleHolder>>any())).thenThrow(new RestClientException("Test exception"));

      

You will receive this error message because the method exchange

does not declare an exception, but the exceptionConnectException

is checked (that is, it should be declared when thrown).

+10


source







All Articles