我正在尝试在Android中对以下方法进行单元测试:
@Override
public void onTryAgain() {
mView.showLoading();
mGetLoginSsoUseCase.execute(
getLoginSsoLink(),
new GetSsoLinkUseCase.Params(mUrl));
}
其中 getLoginSsoLink() 返回一个回调对象:
private ICallback<GetLoginSsoLinkResult> getLoginSsoLink() {
return new ICallback<GetLoginSsoLinkResult>() {
@Override
public void onResult(GetLoginSsoLinkResult result) {
// some code
}
@Override
public void onKoError(KOException exception) {
// some code
}
@Override
public void onConnectivityError(ConnectivityException exception) {
// some code
}
@Override
public void onGenericError(Exception exception) {
// some code
}
};
}
我使用 Mockito 的单元测试是这样的:
@Test
public void WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(){
doNothing().when(mGetSsoLinkUseCase).execute(
FAKE_GET_LOGIN_SSO_LINK_RESULT_CALLBACK,
FAKE_GET_SSO_LINK_USE_CASE_PARAMS);
mPresenter.onTryAgain();
verify(mGetSsoLinkUseCase).execute(
FAKE_GET_LOGIN_SSO_LINK_RESULT_CALLBACK,
FAKE_GET_SSO_LINK_USE_CASE_PARAMS);
}
假变量在哪里:
private static final ICallback<GetLoginSsoLinkResult> FAKE_GET_LOGIN_SSO_LINK_RESULT_CALLBACK =
new ICallback<GetLoginSsoLinkResult>() {
@Override
public void onResult(GetLoginSsoLinkResult result) {}
@Override
public void onKoError(KOException exception) {}
@Override
public void onConnectivityError(ConnectivityException exception) {}
@Override
public void onGenericError(Exception exception) {}
};
private static final GetSsoLinkUseCase.Params FAKE_GET_SSO_LINK_USE_CASE_PARAMS =
new GetSsoLinkUseCase.Params(FAKE_SSO_URL);
如果我运行测试,则会出现以下错误:
论点不同!Wanted: mGetSsoLinkUseCase.execute( com.xxx.xxxforstores.presenter.LoginSsoPresenterTest$1@8317c52, com.xxx.xxxforstores.domain.usecase.GetSsoLinkUseCase$Params@68e965f5 ); -> at com.xxx.xxxforstores.presenter.LoginSsoPresenterTest.WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(LoginSsoPresenterTest.java:111) 实际调用有不同的参数:mGetSsoLinkUseCase.execute( com.xxx.xxxforstores.mvp.presenter.LoginSsoPresenter$1@76f2bbc1, com.xxx.xxxforstores.domain.usecase.GetSsoLinkUseCase$Params@6f27a732 ); -> at com.xxx.xxxforstores.mvp.presenter.LoginSsoPresenter.onTryAgain(LoginSsoPresenter.java:115)
比较失败:
论点不同!Wanted: mGetSsoLinkUseCase.execute( com.xxx.xzzforstores.presenter.LoginSsoPresenterTest$1@8317c52, com.xxx.xxzforstores.domain.usecase.GetSsoLinkUseCase$Params@68e965f5 ); -> at com.xxx.xxxforstores.presenter.LoginSsoPresenterTest.WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(LoginSsoPresenterTest.java:111) 实际调用有不同的参数:mGetSsoLinkUseCase.execute( com.xxx.xxxforstores.mvp.presenter.LoginSsoPresenter$1@76f2bbc1, com.xxx.xxxforstores.domain.usecase.GetSsoLinkUseCase$Params@6f27a732 ); -> at com.xxx.xxxforstores.mvp.presenter.LoginSsoPresenter.onTryAgain(LoginSsoPresenter.java:115)
在 com.xxx.xxxforstores.presenter.LoginSsoPresenterTest.WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(登录SsoPresenterTest.java:111) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal.runners.statement.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78) 在 org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84) 在 org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39) 在 org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 在 com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
我在单元测试方面有一点经验,所以我认为我可能做错了什么,我必须更改什么才能测试这样的方法?
1)如果mGetLoginSsoUseCase
是一个模拟,那么你实际上不需要用doNothing
设置.execute
方法。反正默认情况下不会发生任何事情
2)你不能真正在verify
中寻找精确的对象,因为其中一个参数只是在飞行中创建的:new GetSsoLinkUseCase.Params(mUrl)
。我会进行any(class)
检查,这对您来说应该足够了:
verify(mGetSsoLinkUseCase).execute(
Mockito.any(ICallback.class),
Mockito.any(GetSsoLinkUseCase.Params.class));