android - Verify interactions in rxjava subscribers -
picture situation in mvp pattern presenter subscribes service returning observer:
public void gatherdata(){ service.dosomemagic() .observeon(schedulers.io()) .subscribeon(androidschedulers.mainthread()) .subscribe(new thesubscriber()); }
now class thesubscriber
calls onnext
method view, say:
@override public void onnext(returnvalue value) { view.displaywhatever(value); }
now, in unit test verify when method gatherdata()
called on non-erroneous situation, view's method displaywhatever(value)
called.
the question:
is there clean way this?
background:
- i'm using mockito verify interactions , lot more of course
- dagger injecting entire presenter except
thesubscriber
what have tried:
- inject subscriber , mock in tests. looks bit dirty me, because if want change way presenter interacts service (say not rx) need change lot of tests , code.
- mock entire service. not bad, requires me mock lot of methods , didn't quite reach wanted.
- looked around internet, no 1 seems have clean straight way of doing this
thanks help
assuming using interfaces service
, view
in similar manner:
class presenter{ service service; view view; presenter(service service){ this.service = service; } void bindview(view view){ this.view = view; } void gatherdata(){ service.dosomemagic() .observeon(schedulers.io()) .subscribeon(androidschedulers.mainthread()) .subscribe(view::displayvalue); } }
it possible provide mock control , verify behaviour:
@test void assert_that_displayvalue_is_called(){ service service = mock(service.class); view view = mock(view.class); when(service.dosomemagic()).thenreturn(observable.just("myvalue")); presenter presenter = new presenter(service); presenter.bindview(view); presenter.gatherdata(); verify(view).displayvalue("myvalue"); }
Comments
Post a Comment