java - How to unit-test a presenter in a MVP -


i have model view presenter triad. i'd know what's usual way test presenter.

the first thing came mind instantiate presenter , instantiate real view , assert view accomplish expected behavior.

public void itshouldsayhello() {     view view = new view();     presenter presenter = new presenter(view);     presenter.usersaid("hello");     asserttrue(view.getgreeting().equals("hello")); } 

then thought view not under test , created fake view.

private string greeting;  public void itshouldsayhello() {     view view = new fakeview();     presenter presenter = new presenter(view);     presenter.usersaid("hello");     asserttrue(greeting.equals("hello")); } private class fakeview implements view {     @override     public void displaygreeting(string saluto) {         greeting = saluto;     } } 

then thought interface of view change. would've made code harder maintain. wrote test , asserted presented view. way if interface changed have change 1 line of code in tests.

public void itshouldsayhello() {     view view = mock(view.class);     presenter presenter = new presenter(view);     presenter.usersaid("hello");     verify(view).displaygreeting("hello"); } 

so test expect presenter gather , process information , pass view, verify passed values correct.

so guess i'm not using fake now, i'm using mock , verify if mock receives correct values.

another problem have model. think insurmountable. have see if presenter behaves correctly create big fat fixture. pass various combinations , see if presenter behaves correctly.

how test presenter?

you leverage separation of view/presenter test presenter. if you've implemented mvp fully, view implement interface , presenter use interface return data view. if testing mvp application want not presenter function correctly, interface view uses communicate presenter.

so test class should implement view's interface, invoke methods of presenter, , store responses presenter local methods override interface. if presenter communicates business layer synchronously, easier:

  1. define test class implements view interface
  2. implement interface in test class data prepared presenter stored in test object
  3. create test methods in test class invoke presenter, response object, , perform appropriate comparisons.

if presenter communicates asynchronously have wait in test method , notify interface methods.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -