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:
- define
contactservicereal classnew clientservice - inject somehow mock of
serviceclient(via constructor or setter, did not posted code not know how work together) - since
contactservicereal instance injected mock ofserviceclientgo real method , invokeclient.posttoservicereturntruedefined.
option 2 thencallrealmethod on contactservice mock if need mock (but not see why should mock). like:
when(contactservice.sendemailcontact(cdto)).thencallrealmethod();
Comments
Post a Comment