首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnfinishedStubbingException与PowerMockito

UnfinishedStubbingException与PowerMockito
EN

Stack Overflow用户
提问于 2017-10-21 14:06:58
回答 1查看 1.6K关注 0票数 3

我想测试一种在内部使用静态方法的方法,这就是我使用PowerMockito的原因。我想模拟来自Http的对Request.Get的调用。我设置了一个简单的测试,但是我得到了一个UnfinishedStubbingException,我不明白为什么。

Mockito 2.8.9和Powermock 1.7.1

代码语言:javascript
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest(Request.class)
public class MyApiServiceTest {

    @Test
    public void callTest() throws IOException, JSONException, URISyntaxException {
        PowerMockito.mockStatic(Request.class);
        Path path = Paths.get(getClass().getClassLoader().getResource("data.json").toURI());
        byte[] fileBytes = Files.readAllBytes(path);
        Content content = new Content(fileBytes, ContentType.APPLICATION_JSON);
        PowerMockito.doReturn(content).when(Request.Get(anyString()).execute().returnContent());
    }

}

斯塔克迹

代码语言:javascript
复制
org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.example.service.MyApiServiceTest.callTest(MyApiServiceTest.java:42)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed


    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:182)
    at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:164)
    at org.powermock.core.MockGateway.methodCall(MockGateway.java:141)
    at org.apache.http.client.fluent.Request.Get(Request.java)
    at com.example.service.MyApiServiceTest.callTest(MyApiServiceTest.java:42)
    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.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-21 14:59:00

似乎你在同一时间试图嘲笑太多的事情。

尝试独立地对execute()returnContent()方法进行存根:

代码语言:javascript
复制
@Test
    public void callTest() throws IOException, JSONException, URISyntaxException {
        // Arrange
        PowerMockito.mockStatic(Request.class);
        Request requestStub = Mockito.mock(Request.class);
        Response responseStub = Mockito.mock(Response.class);
        Path path = Paths.get(getClass().getClassLoader().getResource("data.json").toURI());
        byte[] fileBytes = Files.readAllBytes(path);
        Content content = new Content(fileBytes, ContentType.APPLICATION_JSON);

        Mockito.doReturn(responseStub).when(requestStub).execute();
        Mockito.doReturn(content).when(responseStub).returnContent();
        PowerMockito.doReturn(requestStub).when(Request.class, Mockito.anyString(), "Get");

        // Act and assert
    }

所以powermockito只用于返回requestStub,其余的都可以用普通的mockito完成。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46864091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档