cakduro cakduro - 1 year ago 124
Android Question

android testing - mockito error org.mockito.exceptions.misusing.WrongTypeOfReturnValue:

I try to create test for my presenter, but when I run it, I got this kind of error

ScalarSynchronousObservable cannot be returned by getContext()
getContext() should return Context

I create my presenter test class like this

public class CreateTalkPresenterTest {

TalkService talkService;

CreateTalkMvpView createTalkMvpView;

CreateTalkPresenter createTalkPresenter;

public void setUp() throws Exception {
talkService = ServiceFactory.createMapi(createTalkMvpView.getContext(), TalkService.class);
createTalkPresenter = new CreateTalkPresenter(Schedulers.immediate(), Schedulers.immediate());

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

FileUtil.createPartFromString("Lorem Ipsum dolor"),

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


and for Mock the result I use this class

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(); = 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(); = "100"; = name;
return categoryTalkModel;

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

And this is the presenter class that I want to test

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;

public void attachView(CreateTalkMvpView view) {
createTalkMvpView = view;

public void detachView() {
createTalkMvpView = null;

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

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

subscription = mApiTalkService.createNewTalk(
filePath != null ? FileUtil.prepareFilePart("picture", new File(filePath)) : null,
.subscribe(new Subscriber<TalkService.TalkResultModel>() {
public void onCompleted() {

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

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


I'm using retrofit 2.1.0 and rx android in this case.
So if someone have any idea, what I'm doing wrong in my code. Please help me


Answer Source

talkService isn't a mock. Even though you have this set:

TalkService talkService;

You then overwrite it in your @Before method setUp:

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

So in your test, this happens to a real TalkService implementation:

when(talkService.createNewTalk(/* ... */

Which then calls a real createNewTalk method, which starts with this:

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

The rest of the method isn't important, because Mockito's when works by mocking the last method that was called before/within the call to when, and nothing else in that method interacts with mocks. If talkService were a mock, then when(talkService.createNewTalk(/*...*/)) would stub the method createNewTalk, but instead it stubs that last mock method call getContext. This makes it look like:


...which exactly matches your error message:

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

To fix this, just remove your talkService assignment so the when method call is actually a mock, or use a real talkService as you've initialized it and remove the @Mock annotation and when and verify statements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download