java - Mocking a generic class with an abstract version of said class -


i'm attempting mock abstract class, i've seen, don't think it's possible. have classes use generics, must extends specific abstract class. there's whole group of them , have been mocked successfully. abstract class has 1 method deals returning generic , looks this:

public abstract class childpresenter <t extends childview> {     private t view;      public abstract t getview(); } 

the class testing has following in it:

public class parentpresenter {     private concretechildpresenter1 childpresenter1;     private concretechildpresenter2 childpresenter2;     private concretechildpresenter3 childpresenter3;     private concretechildpresenter4 childpresenter4;     list<childpresenter> childpresenters; } 

in constructor, these classes injected in, using google guice, set variables, , added list of child presenters.

the method under test 1 iterates on of childpresenters objects , runs method getview().

i attempted way in test class:

public class parentpresentertest {     private concretechildpresenter1 childpresenter1;     private concretechildpresenter2 childpresenter2;     private concretechildpresenter3 childpresenter3;     private concretechildpresenter4 childpresenter4;     private list<childpresenter> childpresenters;      //this abstract class      private childview childview;      @beforetest     public void createinjector() {         guice.createinjector(...//creates fake module , binding variables mentioned earlier             //e.g.             childpresenter1 = mock(concretechildpresenter1.class);             binder.bind(concretechildpresenter1.class).toinstance(childpresenter1);             //e.t.c other variables              //same child view             childview = mock(childview.class);             binder.bind(childview.class).toinstance(childview);         }          childpresenters = new arraylist<childpresenter>();         childpresenters.add(childpresenter1);         //add child presenters          for(childpresenter childpresenter : childpresenters) {             when(childpresenter.getview()).thenreturn(childview);         }     } } 

the problem happens @ line when(childpresenter.getview()).thenreturn(childview); mockito complains following message:

org.mockito.exceptions.misusing.wrongtypeofreturnvalue:

childview$$enhancerbymockitowithcglib$$2f6a4bd5

cannot returned getview() getview() should return concretechildview1

*** if you're unsure why you're getting above error read on. due nature of syntax above problem might occur because:

  1. this exception might occur in wrongly written multi-threaded tests. please refer mockito faq on limitations of concurrency testing.

  2. a spy stubbed using when(spy.foo()).then() syntax. safer stub spies doreturn|throw() family of methods. more in javadocs mockito.spy() method.

which can understand, seems waste mock each individual concrete childview when want confirm mocked childview called single method using following:

verify(childview, atleast(childpresenters.size())).getview(); 

is there way this? can somehow use mocked abstract classes in place of concrete ones?

edit here concrete version of how getview() method implemented:

public concretechildpresenter1<conretechildview1> {      @override     public conretechildview1 getview() {         view.buildview();         return view;     } } 

and abstract childview class child views extend:

public abstract childview {      public abstract void buildview(); } 

since each child presenter returns view of specific type, can't, understood, substitute them mocks of abstract class childview.

there way @ runtime concrete type of childview if provide proper implementation explained here: get generic type of class @ runtime

then may initialize presenters' mocks in way:

for(childpresenter childpresenter : childpresenters) {     //this getter returns needed runtime class     when(childpresenter.getview()).thenreturn(mock(childpresenter.getviewtype()));  } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -