Mockito然后Throw抛出mockito异常

时间:2015-08-10 08:11:34

标签: java mockito

我要模拟restOperation以抛出异常,但我得到MockitoException而不是采取适当的异常。这对我来说很奇怪,因为当我试图测试快乐路径几乎相同的模拟调用工作正常。这有什么问题:

工作模拟:

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

模拟投掷MockitoException:

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

这是我的方法实现:

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();

堆栈追踪:

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

1 个答案:

答案 0 :(得分:11)

  

引起:org.mockito.exceptions.base.MockitoException:   已检查的异常对此方法无效!   无效:java.net.ConnectException:测试异常

如异常消息所述(并在评论中说明),您必须抛出RestClientException,而不是ConnectException

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

您收到此错误消息,因为方法exchange声明没有抛出异常而ConnectExceptionchecked exception(即必须在抛出时声明)。