PowerMockito 间谍调用真实方法获取 NullPointerException



从间谍类调用真正的方法service.listObjectA("abc")时,我得到了空指针异常,它指向原始类的 ArrayList 起始行List<ObjectASummary> objAList = new ArrayList<>()

我在 IDE 中调试了它,并截取了堆栈跟踪详细信息的屏幕截图。
它说Method threw 'java.lang.NullPointerException' exception. Cannot evaluate net.bytebuddy.description.type.TypeDescription$ForLoadedType.toString()

我还被告知要粘贴堆栈跟踪,所以就在这里。

java.lang.NullPointerException
    at com.myorg.myproject.service.ObjectAService.listObjectA(ObjectAService.java:181)
    at com.myorg.myproject.test.testcases.api.unit.ObjectAServiceTest.testListObjectA(ObjectAServiceTest.java:95)
    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:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    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:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    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:117)
    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:157)
    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)

对象服务.java第 181 行:List<ObjectASummary> objAList = new ArrayList<>();

以下是要测试的类:

public class ObjectAService {
    public ObjectAService(ObjectADataProvider dataProvider, WorkRequestService workRequestService, ObjectBServiceInterface objBService, ObjectCFactory objCFactory, SWConfig swConfig) {
        //constructor
    }
    @Override
    public List<ObjectASummary> listObjectA(String s) {           
        List<ObjectASummary> objAList = new ArrayList<>();//NullPointerException thrown from this ArrayList initiation line
        /*
        Code to query backend for the list and process them.
        The back end query client is mocked and behaviors are stubbed in the test.
        */
        return objAList;
    }
}

这是测试:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ObjectAService.class, RealmIdFactory.class})
@PowerMockIgnore({ "javax.net.ssl.*", "javax.security.*" })
public class ObjectATest {
    private SystemClient swClient;
    private ManagedInstanceService service;
    public ObjectATest() {
        service = PowerMockito.spy(new ObjectAService(null, null, null, null, new SWConfig()));
        swClient = PowerMockito.mock(SystemClient.class);
    }
    @Test
    public void testListObjectA() throws Exception {
        List<ClientSystem> systems = new ArrayList(listSize);
        for(int i = 0; i < 10; i++) {
            systems.add(new ClientSystem());
        }
        PowerMockito.doReturn(swClient).when(service,"getSystemClient");
        PowerMockito.doReturn("xxx.yyy.zzz").when(service,"getObjectAOcid", anyString());
        PowerMockito.doReturn(ObjectADetails.Status.Normal).when(service, "getClientSystemOsaStatus", any(SystemClient.class), anyInt());
        PowerMockito.when(swClient.listSystems()).thenReturn(systems);
        List<ObjectASummary> list = service.listObjectA("abc");
        assertNotNull(list);
    }
}

我不知道你为什么要使用 PowerMock,但这里对我来说没有用。PowerMock的使用非常具体,背后通常存在概念问题。

我建议你简单地单独使用Mockito,看看问题是否仍然存在。

如果问题消失,则向开发团队报告。

相关内容

  • 没有找到相关文章

最新更新