java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -


my test

list<person> mylist;  @test public void testisvalidperson() {     mylist = new arraylist<person>();      mylist.add(new person("tom"));     when(persondao.get(person)).thenreturn(mylist);     when((persondao.get(person)).isempty()).thenreturn(false);//------exception thrown       boolean result = service.isvalid("tom");     assertfalse(result); } 

method tested:

public boolean isvalid(string person){     persondao = new persondao();     person personobj = new person(person);     return (persondao.get(person).isempty())?false : true;       } 

exception thrown:

org.mockito.exceptions.misusing.wrongtypeofreturnvalue:  boolean cannot returned get() get() should return list *** if you're unsure why you're getting above error read on. due nature of syntax above problem might occur because: 1. exception *might* occur in wrongly written multi-threaded tests.    please refer mockito faq on limitations of concurrency testing. 2. spy stubbed using when(spy.foo()).then() syntax. safer stub spies -     - doreturn|throw() family of methods. more in javadocs mockito.spy() method. 

my second approach using spy:

public void testisvalidperson() {     mylist = new arraylist<person>();      mylist.add(new person("tom"));     when(persondao.get(person)).thenreturn(mylist);      list<person> mylist = persondao.get(person);     list spy = spy(mylist);      doreturn(false).when(spy.isempty());//------exception thrown      boolean result = service.isvalid("tom");     assertfalse(result); } 

this gives me following exception:

org.mockito.exceptions.misusing.unfinishedstubbingexception:  unfinished stubbing detected here: -> @ com.persontest.testisvalid(persontest.java:76)  e.g. thenreturn() may missing. examples of correct stubbing:     when(mock.isok()).thenreturn(true);     when(mock.isok()).thenthrow(exception);     dothrow(exception).when(mock).somevoidmethod(); hints:  1. missing thenreturn()  2. trying stub final method, naughty developer! 

third appraoch:

@test public void testisvalidperson() {     mylist = new arraylist<person>();      mylist.add(new person("tom"));     when(persondao.get(person)).thenreturn(mylist);      boolean result = service.isvalid("tom");//--------throws null pointer exception     assertfalse(result); }  public boolean isvalid(string person){     persondao = new persondao();     person personobj = new person(person);     return (persondao.get(person).isempty())?false : true;  //----throws npe     } 

fourth approach: throws null pointer exception

@test public void testisvalidperson() {     list<person> mockedlist = mock(list.class);     when(persondao.get(person)).thenreturn(mockedlist);     when(persondao.get(person)).isempty().thenreturn(false);     boolean result = service.isvalid("tom");//--------throws null pointer exception     assertfalse(result); }  public boolean isvalid(string person){     persondao = new persondao();     person personobj = new person(person);     return (persondao.get(person).isempty())?false : true;  //----throws npe     } 

fifth approach: gives npe.

the method of persondao accesses database , npe thrown when getting connection db. doesn't give npe first time around when empty list back. npe in second call service.isvalid()

@test public void testisvalidperson() {     when(persondao.get(person)).thenreturn(new arraylist<person>());     list templist=persondao.get(person);//----i empty templist---no npe     boolean result = service.isvalid("tom");//--------throws null pointer exception     assertfalse(result); } 

approach 6:

@test public void testisvalid() {     personlist = new arraylist<person>();     person person = new person("tom");     personlist.add(person);     when(persondao.get(person)).thenreturn(personlist);//-------uses same person object      boolean result = service.isvalid(person);//------------uses same person object     asserttrue(result); } 

and changed method signature from(so test , method under test use same value).

public boolean isvalid(string name) 

to

public boolean isvalid(person person) 

basically not have enough mocks. attempting mock isempty method of list returned mock object. have mock object, not make mock object return mock list mock methods of mock list ...

one thing fail understand though why attempting make list has elements, returns true isempty

if need test how code behaves when gets empty list, make empty list. way isempty automatically return false.

when(persondao.get(person)).thenreturn(new arraylist<person>()); 

Comments

Popular posts from this blog

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -