java - Mockito return false -


i write simple test on mokito.

public class contactservicetest {      @mock     private serviceclient client;      @mock     private contactservice contactservice;      @before     public void init() {         client = mock(serviceclient.class);         contactservice = mock(contactservice.class);     }      @test     public void test_sendemailcontact() {         contactdto cdto = new contactdto();         cdto.settitle("mr");         cdto.setfirstname("pritam");         cdto.setlastname("mohapatra");         cdto.settelephone("9439586575");         cdto.setemail("pritam.pritam176@gmail.com");         cdto.setbetreff("test value");         cdto.setanfrage("test value");         when(client.posttoservice("customer/sendemailcontact", cdto, boolean.class)).thenreturn(true);         assert.assertequals(true, contactservice.sendemailcontact(cdto));     }  } 
  • but when fail msg expected true return false miss.

you using serviceclient in contactservice send email contact. defined contactservice mock testing mock instead of real class.

option 1 prefered option if contactservice not need mock:

  1. define contactservice real class new clientservice
  2. inject somehow mock of serviceclient (via constructor or setter, did not posted code not know how work together)
  3. since contactservice real instance injected mock of serviceclient go real method , invoke client.posttoservice return true defined.

option 2 thencallrealmethod on contactservice mock if need mock (but not see why should mock). like:

when(contactservice.sendemailcontact(cdto)).thencallrealmethod(); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -