android 回调方法参数,单元测试使用Mockito | Android调用私有回调的方法

我试图在Android中对以下方法进行单元测试:

@Override

public void onTryAgain() {

mView.showLoading();

mGetLoginSsoUseCase.execute(

getLoginSsoLink(),

new GetSsoLinkUseCase.Params(mUrl));

}

其中getLoginSsoLink()返回一个回调对象:

private ICallback getLoginSsoLink() {

return new ICallback() {

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

}

假vars的地方是:

private static final ICallback FAKE_GET_LOGIN_SSO_LINK_RESULT_CALLBACK =

new ICallback() {

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

如果我运行测试,我会收到以下错误:

参数不同!通缉:mGetSsoLinkUseCase.execute(com.whisbi.whisbiforstores.presenter.LoginSsoPresenterTest$1@8317c52,com.whisbi.whisbiforstores.domain.usecase.GetSsoLinkUseCase$Params@68e965f5); - > at com.whisbi.whisbiforstores.presenter.LoginSsoPresenterTest.WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(LoginSsoPresenterTest.java:111)实际调用有不同的参数:mGetSsoLinkUseCase.execute(com.whisbi.whisbiforstores.mvp.presenter.LoginSsoPresenter$1@76f2bbc1,com.whisbi . whisbiforstores.domain.usecase.GetSsoLinkUseCase$Params@6f27a732); - > at com.whisbi.whisbiforstores.mvp.presenter.LoginSsoPresenter.onTryAgain(LoginSsoPresenter.java:115)比较失败:参数不同!通缉:mGetSsoLinkUseCase.execute(com.whisbi.whisbiforstores.presenter.LoginSsoPresenterTest$1@8317c52,com.whisbi.whisbiforstores.domain.usecase.GetSsoLinkUseCase$Params@68e965f5); - > at com.whisbi.whisbiforstores.presenter.LoginSsoPresenterTest.WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(LoginSsoPresenterTest.java:111)实际调用有不同的参数:mGetSsoLinkUseCase.execute(com.whisbi.whisbiforstores.mvp.presenter.LoginSsoPresenter$1@76f2bbc1,com.whisbi . whisbiforstores.domain.usecase.GetSsoLinkUseCase$Params@6f27a732); - >在com.whisbi.whisbiforstores.mvp.presenter.LoginSsoPresenter.onTryAgain(LoginSsoPresenter.java:115)在com.whisbi.whisbiforstores.presenter.LoginSsoPresenterTest.WhenCallOnTryAgain_ThenShowLoadingIsCalledInView(LoginSsoPresenterTest.java:111)在sun.reflect.NativeMethodAccessorImpl.invoke0 (本地方法)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) )在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.statements.RunBefores.evaluate(RunBefores.java :26)在org.junit.runners.ParentRunner.runLeaf (ParentRunner.java:325)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)org.junit.runners.ParentRunner $ 3 .RUN(ParentRunner.java:290)在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在org.junit.runners .ParentRunner.access $ 000(ParentRunner.java:58)维持在组织org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在org.junit.runners.ParentRunner.run(ParentRunner.java:363) . 在org.mockito.runners.runners.StrictRunner.run(StrictRunner)的org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84)中的mockito.internal.runners.DefaultInternalRunner $ 1.run(DefaultInternalRunner.java:78)的.java:39)在org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)在org.junit.runner.JUnitCore.run(JUnitCore.java:137)在com.intellij.junit4.JUnit4Idea TestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)at com.intellij.rt.exe.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)at 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)在java.lang.reflect.Method.invoke(Method.java:498)在com.intellij.rt.execution.application.AppMainV2.main(AppMainV2的.java:131)

我在单元测试方面有一点经验,所以我认为我可能做错了什么,我必须改变以测试一个类似的方法那?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值