为什么 Mockito 在参数正确时会抱怨 misusing.WrongTypeOfReturnValue?
Why does Mockito complain about misusing.WrongTypeOfReturnValue when the parameters are correct?
我 运行 我的登录单元测试一直返回错误 :
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
ObservableJust cannot be returned by doServerLoginApiCall()
doServerLoginApiCall() should return Single
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
我不确定为什么会抛出错误,即使我交叉检查了所有内容都是正确的,尽管由于上述原因测试一直失败。这是我的代码:
登录演示者测试:
@RunWith(MockitoJUnitRunner.class)
public class LoginPresenterTest {
@Mock
LoginMvpView mMockLoginMvpView;
@Mock
DataManager mMockDataManager;
private LoginPresenter<LoginMvpView> mLoginPresenter;
private TestScheduler mTestScheduler;
@BeforeClass
public static void onlyOnce() throws Exception {
}
@Before
public void setUp() throws Exception {
CompositeDisposable compositeDisposable = new CompositeDisposable();
mTestScheduler = new TestScheduler();
TestSchedulerProvider testSchedulerProvider = new TestSchedulerProvider(mTestScheduler);
mLoginPresenter = new LoginPresenter<>(
mMockDataManager,
testSchedulerProvider,
compositeDisposable);
mLoginPresenter.onAttach(mMockLoginMvpView);
}
@Test
public void testServerLoginSuccess() {
String email = "dummy@gmail.com";
String password = "password";
LoginResponse loginResponse = new LoginResponse();
doReturn(Observable.just(loginResponse))
.when(mMockDataManager)
.doServerLoginApiCall(new LoginRequest
.ServerLoginRequest(email, password));
mLoginPresenter.onServerLoginClick(email, password);
mTestScheduler.triggerActions();
verify(mMockLoginMvpView).showLoading();
verify(mMockLoginMvpView).hideLoading();
verify(mMockLoginMvpView).openMainActivity();
}
@After
public void tearDown() throws Exception {
mLoginPresenter.onDetach();
}
这是我的测试计划提供者:
public class TestSchedulerProvider implements SchedulerProvider {
private final TestScheduler mTestScheduler;
public TestSchedulerProvider(TestScheduler testScheduler) {
this.mTestScheduler = testScheduler;
}
@Override
public Scheduler ui() {
return mTestScheduler;
}
@Override
public Scheduler computation() {
return mTestScheduler;
}
@Override
public Scheduler io() {
return mTestScheduler;
}
}
LoginPresentertest 在第
行抛出错误
.doServerLoginApiCall(new LoginRequest
.ServerLoginRequest(email, password));
知道如何改变它才能工作吗?
谢谢!
显然你的 doServerLoginApiCall
returns 单身,但你试图用 Observable.just(loginResponse)
来嘲笑它。它应该类似于 Single.just(loginResponse)
我 运行 我的登录单元测试一直返回错误 :
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
ObservableJust cannot be returned by doServerLoginApiCall()
doServerLoginApiCall() should return Single
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
我不确定为什么会抛出错误,即使我交叉检查了所有内容都是正确的,尽管由于上述原因测试一直失败。这是我的代码: 登录演示者测试:
@RunWith(MockitoJUnitRunner.class)
public class LoginPresenterTest {
@Mock
LoginMvpView mMockLoginMvpView;
@Mock
DataManager mMockDataManager;
private LoginPresenter<LoginMvpView> mLoginPresenter;
private TestScheduler mTestScheduler;
@BeforeClass
public static void onlyOnce() throws Exception {
}
@Before
public void setUp() throws Exception {
CompositeDisposable compositeDisposable = new CompositeDisposable();
mTestScheduler = new TestScheduler();
TestSchedulerProvider testSchedulerProvider = new TestSchedulerProvider(mTestScheduler);
mLoginPresenter = new LoginPresenter<>(
mMockDataManager,
testSchedulerProvider,
compositeDisposable);
mLoginPresenter.onAttach(mMockLoginMvpView);
}
@Test
public void testServerLoginSuccess() {
String email = "dummy@gmail.com";
String password = "password";
LoginResponse loginResponse = new LoginResponse();
doReturn(Observable.just(loginResponse))
.when(mMockDataManager)
.doServerLoginApiCall(new LoginRequest
.ServerLoginRequest(email, password));
mLoginPresenter.onServerLoginClick(email, password);
mTestScheduler.triggerActions();
verify(mMockLoginMvpView).showLoading();
verify(mMockLoginMvpView).hideLoading();
verify(mMockLoginMvpView).openMainActivity();
}
@After
public void tearDown() throws Exception {
mLoginPresenter.onDetach();
}
这是我的测试计划提供者:
public class TestSchedulerProvider implements SchedulerProvider {
private final TestScheduler mTestScheduler;
public TestSchedulerProvider(TestScheduler testScheduler) {
this.mTestScheduler = testScheduler;
}
@Override
public Scheduler ui() {
return mTestScheduler;
}
@Override
public Scheduler computation() {
return mTestScheduler;
}
@Override
public Scheduler io() {
return mTestScheduler;
}
}
LoginPresentertest 在第
行抛出错误 .doServerLoginApiCall(new LoginRequest
.ServerLoginRequest(email, password));
知道如何改变它才能工作吗?
谢谢!
显然你的 doServerLoginApiCall
returns 单身,但你试图用 Observable.just(loginResponse)
来嘲笑它。它应该类似于 Single.just(loginResponse)