PowerMockito不模拟静态类

时间:2019-03-09 13:48:55

标签: android unit-testing mocking powermockito

我需要测试一些使用改造来调用某些远程API的方法。 此方法从实用程序类内部调用静态方法,该实用程序类从firebase中检索字符串,如baseUrl和终结点。

这是在方法makeRequest中调用静态方法FirebaseManager.employeeURL的类

public  class EmployeeAPI extends DedalusHandlerApi<List<Employee>> {
    private String cf;

    public EmployeeAPI(String cf, DedalusApiCallbackListener<List<Employee>> listener) {
        super(listener);
        this.cf = cf;

        doRequest();
    }

    @NotNull
    @Override
    protected Call<List<Employee>> makeRequest() {
        String url = FirebaseManager.employeeURL(this.cf);
        return getApiServiceAuth().getEmployee(url);
    }
}

这是一个引起异常的测试类。

@PrepareForTest(FirebaseManager::class)
@RunWith(PowerMockRunner::class)
@PowerMockIgnore("*")
class WSManagerTest {

private val mockWebServer = MockWebServer()

@Before
fun init() {
    PowerMockito.mockStatic(FirebaseManager::class.java)
    PowerMockito.`when`(FirebaseManager.baseURL()).thenReturn("")
    PowerMockito.`when`(FirebaseManager.loginURL()).thenReturn("")

    mockWebServer.start()
}

这是个例外

java.lang.NullPointerException
at bitdrome.dedalusER.managers.FirebaseManager.getValue(FirebaseManager.java:12)
at bitdrome.dedalusER.ui.home.WSManagerTest.init(WSManagerTest.kt:28)
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.MethodRoadie.runBefores(MethodRoadie.java:133)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
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:282)
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:202)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:144)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
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:120)
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:53)
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)
Disconnected from the target VM, address: '127.0.0.1:50533', transport: 'socket'
Process finished with exit code 255

和FirebaseManager类

public class FirebaseManager {

private static BaseApplication baseApplication;

private static String getValue(String key) {
    if (baseApplication == null) baseApplication = BaseApplication.getInstance();

    return baseApplication.getFirebaseRemoteConfig().getString(key);
}

public static String baseURL() {
    return getValue("WS_BASE_URL");
}

public static String loginURL() {
    return getValue("WS_API_LOGIN");
}

public static String employeeURL(String cf) {
    return String.format("%s/%s",
            getValue("WS_API_EMPLOYEE"),
            cf
    );
}
}

我不明白我什么时候错了。

有人可以帮助我吗?非常感谢。

0 个答案:

没有答案