gpt4 book ai didi

java - android测试-mockito错误org.mockito.exceptions.misusing.WrongTypeOfReturnValue :

转载 作者:行者123 更新时间:2023-12-01 09:18:21 36 4
gpt4 key购买 nike

我尝试为我的演示者创建测试,但是当我运行它时,我收到了这种错误

org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
ScalarSynchronousObservable cannot be returned by getContext()
getContext() should return Context

我像这样创建我的演示者测试类

public class CreateTalkPresenterTest {

@Mock
TalkService talkService;

@Mock
CreateTalkMvpView createTalkMvpView;

CreateTalkPresenter createTalkPresenter;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
talkService = ServiceFactory.createMapi(createTalkMvpView.getContext(), TalkService.class);
createTalkPresenter = new CreateTalkPresenter(Schedulers.immediate(), Schedulers.immediate());
createTalkPresenter.attachView(createTalkMvpView);
}

@Test
public void createTalkSuccessfullTest() {
TalkService.TalkResultModel talkResultModel = MockModel.newTalkResultModel();

when(talkService.createNewTalk(
FileUtil.createPartFromString("Lorem Ipsum dolor"),
FileUtil.createPartFromString("100"),
null,
FileUtil.createPartFromString("0")
)).thenReturn(Observable.just(talkResultModel));

createTalkPresenter.callCreateTalk("Lorem Ipsum dolor", "100", null);

verify(createTalkMvpView).showProgressIndeterminate();
verify(createTalkMvpView).hideProgressIndeterminate();
verify(createTalkMvpView).showTalkCreated(talkResultModel.object);
}
}

对于模拟结果,我使用此类

public class MockModel {

public static TalkService.TalkResultModel newTalkResultModel(){
TalkService.TalkResultModel talkResultModel = new TalkService.TalkResultModel();
talkResultModel.code = 600;
talkResultModel.message = "Successfully executed!";
talkResultModel.object = newTalkModel();

return talkResultModel;
}

public static TalkModel newTalkModel(){
Random random = new Random();
String index = String.valueOf(random.nextInt(100));
TalkModel talkModel = new TalkModel();
talkModel.id = index;
talkModel.content = "Lorem Ipsum dolor";
talkModel.categorytalk = newCategoryTalkModel("Category "+index);
talkModel.creator = newConsumerModel("User "+index);
return talkModel;
}

public static CategoryTalkModel newCategoryTalkModel(String name){
CategoryTalkModel categoryTalkModel = new CategoryTalkModel();
categoryTalkModel.id = "100";
categoryTalkModel.name = name;
return categoryTalkModel;
}

public static ConsumerModel newConsumerModel(String name){
Random random = new Random();
String index = String.valueOf(random.nextInt(100));
ConsumerModel consumerModel = new ConsumerModel();
consumerModel.id = index;
consumerModel.username = name;
consumerModel.email = name+"@domain.com";
consumerModel.fullName = "Fullname "+name;
return consumerModel;
}
}

这是我要测试的演示者类

public class CreateTalkPresenter implements Presenter<CreateTalkMvpView> {

private CreateTalkMvpView createTalkMvpView;
private TalkService mApiTalkService;
private TalkService.TalkResultModel talkResultModel;
private final Scheduler mainScheduler, ioScheduler;

private Subscription subscription;

public CreateTalkPresenter(Scheduler ioScheduler, Scheduler mainScheduler) {
this.ioScheduler = ioScheduler;
this.mainScheduler = mainScheduler;
}

@Override
public void attachView(CreateTalkMvpView view) {
createTalkMvpView = view;
}

@Override
public void detachView() {
createTalkMvpView = null;
unsubscribe();
}

private void unsubscribe() {
if (subscription != null) subscription.unsubscribe();
}

public void callCreateTalk(String content, String categoryId, String filePath) {
mApiTalkService = ServiceFactory.createMapi(createTalkMvpView.getContext(), TalkService.class);

unsubscribe();
createTalkMvpView.showProgressIndeterminate();
subscription = mApiTalkService.createNewTalk(
FileUtil.createPartFromString(content),
FileUtil.createPartFromString(categoryId),
filePath != null ? FileUtil.prepareFilePart("picture", new File(filePath)) : null,
FileUtil.createPartFromString("0"))
.observeOn(mainScheduler)
.subscribeOn(ioScheduler)
.subscribe(new Subscriber<TalkService.TalkResultModel>() {
@Override
public void onCompleted() {
createTalkMvpView.hideProgressIndeterminate();
createTalkMvpView.showTalkCreated(talkResultModel.object);
}

@Override
public void onError(Throwable e) {
createTalkMvpView.hideProgressIndeterminate();
WarningUtil.onApiError(createTalkMvpView.getContext(), 0, e.getMessage());
}

@Override
public void onNext(TalkService.TalkResultModel talkResultModel) {
CreateTalkPresenter.this.talkResultModel = talkResultModel;
}
});

}
}

在本例中我使用的是 Retrofit 2.1.0 和 rx android。因此,如果有人知道我的代码中做错了什么。请帮助我

谢谢。

最佳答案

talkService 不是一个模拟。即使您有这套:

@Mock
TalkService talkService;

然后,您可以在 @Before 方法 setUp 中覆盖它:

talkService = ServiceFactory.createMapi(createTalkMvpView.getContext(), TalkService.class);

因此,在您的测试中,这种情况发生在真正的 TalkService 实现中:

when(talkService.createNewTalk(/* ... */
)).thenReturn(Observable.just(talkResultModel));

然后调用一个真正的createNewTalk方法,该方法以此开头:

mApiTalkService = ServiceFactory.createMapi(
createTalkMvpView.getContext(), TalkService.class);

该方法的其余部分并不重要,因为 Mockito's when works by mocking the last method that was called before/within the call to when ,并且该方法中没有其他内容与模拟交互。如果 talkService 是一个模拟,则 when(talkService.createNewTalk(/*...*/)) 会 stub 方法 createNewTalk,但是相反,它会 stub 最后一个模拟方法调用 getContext。这使它看起来像:

when(createTalkMvpView.getContext()).thenReturn(Observable.just(talkResultModel));

...与您的错误消息完全匹配:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: ScalarSynchronousObservable cannot be returned by getContext()

<小时/>

要解决此问题,只需删除您的 talkService 分配,以便 when 方法调用实际上是一个模拟,或者使用真正的 talkService 作为您已对其进行初始化并删除 @Mock 注释以及 whenverify 语句。

关于java - android测试-mockito错误org.mockito.exceptions.misusing.WrongTypeOfReturnValue :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40353920/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com