junit - @Mock objects returning null -


so have code below-

@runwith(mockitojunitrunner.class) public class teamsubscriptionservicetest {      @injectmocks     private teamsubscriptionservice teamsubscriptionservice;      @mock     private imscustomerprofileservice imsservice;      @mock     private iuserservice userservice;      @mock     private httprequestservice httprequestservice;      @mock     private isubscriptiondbservice subscriptiondbservice;      private string imstoken = "ims_token";      @before     public void setup() {         mockitoannotations.initmocks(this);         when(imsservice.getaccesstoken()).thenreturn(imstoken);         reflectiontestutils.setfield(teamsubscriptionservice, "jilendpoint", "www.testjil.com");         reflectiontestutils.setfield(teamsubscriptionservice, "adobeioapikey", "api_key");     }       @test(groups = { testgroup.unit_tests })     public void testaddseat() throws ioexception {          string teamid = "testteamid";         string locale = "en_us";         string jasonvalue = "testjasondata";         string apicallcontent = "addseatapiresult";          httpresponse addseatresponse  = mock(httpresponse.class);         when(addseatresponse.getcode()).thenreturn(200);         when(addseatresponse.getcontent()).thenreturn(apicallcontent);          httpservletresponse response = mock(httpservletresponse.class);         when(httprequestservice.makehttprequest(anystring(),anystring(),anymap(),anystring())).thenreturn(addseatresponse);          string result = teamsubscriptionservice.addseat(teamid,locale,jasonvalue,response);         assertnotnull(result);         assertequals(result, "addseatapiresult");     } } 

when test nullpointerexception on line

when(httprequestservice.makehttprequest(anystring(),anystring(),anymap(),anystring())).thenreturn(addseatresponse); 

i feel objects annotated @mock somehow null , object not getting injected teamsubscriptionservice object.

any idea whats wrong code?

the problem mixing testng , junit annotations.

test method annotated @test(groups = { testgroup.unit_tests }) - testng annotation @org.testng.annotations.test, because junit's equivalent not have element called groups.

however, using junit's @before annotation on setup() method, therefore method never invoked. testng equivalent annotation @org.testng.annotations.beforetest. use instead.

<...> import org.mockito.injectmocks; import org.mockito.mock; import org.mockito.mockitoannotations import org.testng.annotations.beforetest; import org.testng.annotations.test; <...>  public class teamsubscriptionservicetest {     @injectmocks     private teamsubscriptionservice teamsubscriptionservice;     @mock     private imscustomerprofileservice imsservice;     @mock     private iuserservice userservice;     @mock     private httprequestservice httprequestservice;     @mock     private isubscriptiondbservice subscriptiondbservice;      private string imstoken = "ims_token";      @beforetest     public void setup() {         mockitoannotations.initmocks(this);         <...>     }      @test(groups = { testgroup.unit_tests })     public void testaddseat() throws ioexception {         <...>     } } 

as side note, @runwith(mockitojunitrunner.class) redundant well, when using testng.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -