java - 无法模拟类,抛出异常

标签 java unit-testing junit mockito powermock

需要帮助了解我的单元测试出了什么问题 @测试 公共(public)无效testDownloadSolutionFileAlreadyPresentInLocalRepo()抛出异常{ String downloadUrl = "https://artifactory.com/artifactory/com/test-artifact/1.0.0/test-artifact-1.0.0.zip "; 字符串路径=“com/test-artifact/1.0.0/”; PowerMockito.mockStatic(ApplicationContext.class); ApplicationContext 实例 = PowerMockito.mock(ApplicationContext.class); PowerMockito.when(ApplicationContext.getInstance()).thenReturn(instance); PowerMockito.when(instance.getRepositoryDirectory()).thenReturn("\repository");

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

SC.class 内部

    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,我不明白为什么。我已经 mock 了它,为什么它显示为空? 如果我删除这一行,PowerMockito.whenNew(SafeFile.class).withAnyArguments().thenReturn(file1); ,那么 rootRepoDir 的值为非空

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

最佳答案

你有:

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

您还使用mockStatic关于ApplicationContext类:

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

不过,您不会对该类执行任何 stub ,因此当您调用 ApplicationContext.getInstance() 时您将得到默认返回值 NULL。然后你尝试调用NULL.getRepositoryDirectory()因此 NPE。

因此请确保在调用 SUT 之前进行设置:

PowerMockito.when(ApplicationContext.getInstance()).thenReturn(appCtxStub);

关于java - 无法模拟类,抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57346011/

相关文章:

java - Spock:setup() cleanup() 方法中的测试名称和结果

c# - 我是否只需要在 Mock 对象上显式设置预期的返回值?

java - 注入(inject)命名的 Guice 单例

java - LongAccumulator 没有得到正确的结果

reactjs - 如何使用 __mocks__ 目录中的 jest 来模拟 React-router-dom

java - Android Content Provider 测试真实的内容提供者

java - 递归循环 Selenium Webdriver

java - Deeplearning4j 示例不适用于 gradle

Java 将 float 保留 2 位小数

java - 如何进行 Java (Android) 字符串替换?