当从间谍类调用真实方法service.listObjectA("abc")
时,我得到了空指针异常,该异常指向原始类的ArrayList启动行List<ObjectASummary> objAList = new ArrayList<>()
。
我在IDE中进行了调试,并获取了screenshot的堆栈跟踪详细信息。
它说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)
ObjectAService.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);
}
}
答案 0 :(得分:0)
我不知道您为什么使用PowerMock,但是这里没有用。 PowerMock的用法非常具体,通常背后存在概念问题。
我建议您仅单独使用Mockito,看看问题是否仍然存在。
如果问题消失,则将其报告给开发团队。