junit - Mockito: How to test if another class' method is called within a method of a Mock? -


so have service class.

public class organisationservice {     public list<organisation> findallforeignorganisations() {         // few rows of jpql-code searches database         return mycriteria.getresultlist();     }     //...other methods return lists other types of organisations... } 

then have class want test. want test is, when following class's method getallowedorganisations given parameter (organisationtype.foreign in case), uses aforementioned method findallforeignorganisation() search list of organisations.

public class organisationselectionoptions {  @inject organisationservice os;  public list<organisation> getallowedorganisations(assignment a) {     organisationtype type = a.getorganisationtype();     return giveorganisationlistwithtype(type); }  private list<organisation> giveorganisationlistwithtype(organisationtype type) {     list<organisation> organisations;     if (type == organisationtype.foreign) {         organisations = os.findallforeignorganisations();     }     // ...some other organisations types on if-clauses...     return organisations;     } }  } 

i made junit test test this. have create mocks of these aforementioned classes because access database , can't access database in tests , avoid implementing database structure tests.

@runwith(arquillian.class) @preparefortest(organisation.class) public class organisationselectiontest  {     @test     public void testwithforeigntype() throws rreflectionutilexception {         // ...code create dummy assignment foreign type   organisation...      mockedorganisationservice = mock(organisationservice.class);     mockedorganisationselectionoptions = mock(organisationselectionoptions.class);       } } 

now tried mockito's verify somehow test, if mockedorganisationselectionoptions.giveorganisationlistwithtype(dummyassignmentwithforeigntypeorganisation) call os.findallforeignorganisations(); no avail.

i'm clueless beginner on creating tests. vastly appreciated , rewarded, thank you. i'm ready answer comments/questions give more detail on issue.

this can done via multiple ways go on one. first, because use @inject instance of organisationservice, not mess around mocking new organisationservice() constructor call. instead can setup getter method organisationservice, mock it.

add getter in organisationselectionoptions .

organisationservice getorganisationservice(){     return os; } 

edit giveorganisationlistwithtype method use our new getter.

organisations = getos().findallforeignorganisations(); 

in unit test mock instance of organisationservice. there 2 ways this.

method one: add following in toimeksiannonyhtiovalintatest.

@mock  private organisationservice mockos; 

method two: instantiate mockos in method doing testing.

organisationservice mockos = mock(organisationservice.class); 

for method 1 work need include..

@runwith(mockitojunitrunner.class) 

in toimeksiannonyhtiovalintatest. class level annotation.

next need spy object being tested.

organisationselectionoptions totest = spy( new organisationselectionoptions ()); 

then mock our new getter method.

when(totest.getorganisationservice()).thenreturn(mockos); 

there go.

edit: added...

edit giveorganisationlistwithtype method use our new getter.

organisations = getos().findallforeignorganisations(); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -