android - Unit Test - Mockito gives NullPointerException when mocking an interface -


i have presenter, has method getview() returns view. view, implements myview (an interface), has method isactive indicates whether active.

i testing presenter.

in @before setup of testclass, initialize presenter.

testclass.java:

@runwith(androidjunit4.class) public class testclass {     private presenter presenter;     @mock     private myview mockview;  @before public void setup() {     mockitoannotations.initmocks(this);     presenter = new presenter(mockview);     when(mockview.isactive()).thenreturn(true); }  @test public void testisactive() {     presenter.isviewactive();     verify(mockview).isactive(); } 

presenter.java:

public class presenter {     private myview view;      // following view instance of myview, not view (typo before).     public presenter(myview view) {         this.view = view;     }      public boolean isviewactive() {         return getview().isactive();     }      public myview getview() {         return view;     }  } 

myview.class:

public interface myview {     boolean isactive(); } 

however, above code generate nullpointerexception when

presenter.isviewactive(); 

is running.

why it?

and when replace

@mock private myview mockview; 

with

@mock private concretemyview mockview; 

where concretemyview implementation of interface myview, code runs smoothly.

any hints?

i mdewit identified tested class taking view (not shown/defined) instead of myview.

if have problem can in code below , compare.

public class sometest {   private presenter presenter;   @mock   private myview mockview;    @before   public void setup() {     mockitoannotations.initmocks(this);     presenter = new presenter(mockview);     when(mockview.isactive()).thenreturn(true);   }    @test   public void testisactive() {     asserttrue(presenter.isviewactive());     verify(mockview).isactive();   }    private class presenter {     private myview view;      public presenter(myview view) {       this.view = view;     }      public boolean isviewactive() {       return getview().isactive();       //return true;     }      public myview getview() {       return view;     }   }    private interface myview {     boolean isactive();   } } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -