当前位置: 首页 > 知识库问答 >
问题:

无法模拟类,引发异常

糜鸿风
2023-03-14
    SafeFile safeFile = PowerMockito.mock(SafeFile.class);
    PowerMockito.whenNew(SafeFile.class).withArguments(ApplicationContext.getInstance().getReportsDirectory()).thenReturn(safeFile);
    when(safeFile.getChild(Mockito.any())).thenReturn(safeFile);
    SafeFile result = subsystem.downloadSolution(downloadUrl, path);
    Assert.assertEquals(safeFile, result);
}
    SafeFile downloadSolution(String downloadUrl, String path) throws Exception {
        try {
            String fileName = downloadUrl.substring(downloadUrl.lastIndexOf(SharedConstants.PATH_DELIMITER) + 1, downloadUrl.length());
            SafeFile rootRepoDir = new SafeFile(ApplicationContext.getInstance().getRepositoryDirectory());
            String pkgSolutionPath = "DownloadedSolutions" + SharedConstants.PATH_DELIMITER + path;
            SafeFile tempFile = rootRepoDir.getChild(pkgSolutionPath + fileName);
            if (tempFile.exists()) {
                return tempFile;
            } else {
some logic ...
return new_file;}
java.lang.Exception
    at com.system.subsystems.solutioncentral.SC.downloadSolution(SC.java:1017)
    at com.system.subsystems.solutioncentral.SCTest.testDownloadSolutionFileAlreadyPresentInLocalRepo(SCTest.java:832)
    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.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

单元测试以空指针异常退出。SafeFile rootRepoDir的值为null,我不知道为什么。我嘲笑过它,那为什么它显示为null?如果我删除这一行,powerMockito.whennew(Safefile.class).withanyArguments().thenreturn(file1);则rootRepoDir的值为非空

我在PrepareForTest中添加了sc.class,使用PockerMockRunner运行

共有1个答案

龙佐
2023-03-14

您有:

new SafeFile(ApplicationContext.getInstance().getRepositoryDirectory());

还可以在ApplicationContext类上使用MockStatic:

PowerMockito.mockStatic(ApplicationContext.class);
ApplicationContext mockedApplicationContext = Mockito.mock(ApplicationContext.class);

但是,您不对该类执行任何stubbing,所以当您调用applicationcontext.getinstance()时,您将得到默认的返回值NULL。然后尝试调用null.getRepositoryDirectory(),从而调用NPE。

PowerMockito.when(ApplicationContext.getInstance()).thenReturn(appCtxStub);
 类似资料:
  • 问题内容: 我正在使用EasyMock编写许多协作类的单元测试。这些类之一(称为)打开了到远程服务器的网络连接,并将该服务器的XML响应解析为其余类可以使用的东西。 目前,我的测试仅涉及所有情况都是笨拙且远程服务器已启动并正在运行并按预期返回XML的情况。但是,如果可以进行模拟,以便模拟远程服务器关闭或发生其他问题导致抛出,该怎么办,我 会 更高兴。 我看过EasyMock API,但看不到任何看

  • 问题内容: 我正在尝试使用Powermock和Mockito模拟无效的静态方法来引发异常,如下所示。但是我遇到了一个问题。除非我使用相同的参数对Adder.add()进行两次调用,否则不会抛出模拟对象。 顺便说一句,我已经添加并在单元测试类。 提前致谢。:) 答案如下。 在这里http://code.google.com/p/powermock/issues/detail?id=278进行咨询之后

  • 当方法运行时,我希望抛出一个异常(在测试时)。我能做的事情很少: 存根(mock.someMethod(“某些参数”)).ToThrow(new RuntimeException()); 当(mock.someMethod(“某些参数”)).thenThrow(new RuntimeException()) 放弃..... 通常我会创建一个spy对象来调用spied方法。使用stubbing我可以

  • 我对JUnit测试用例有问题。由于某些原因,我无法进入模拟类的一个方法,而且我不确定为什么会发生这种情况。 我甚至不打算用下面的方法。我不确定为什么会发生这种情况,这就给了我上面的结论。

  • 我试图在测试中模拟一个调用,但我得到了一个错误,因为它调用了真正的方法,而不是模拟它。 这是我的方法 } 这是我的测试课 测试实际上调用了受保护的方法config Setter,并在设置代理时失败。帮助我理解我在这里做错了什么。