java - Mock: when() requires an argument which has to be 'a method call on a mock' -


i writing unit tests rest-api , have problems entities creation mocking. don't know how can mock entitymanager. tried example below got error.

my controllertest:

public class mcontrollertest {      private mockmvc mockmvc;      @injectmocks a;     @injectmocks b b;     @injectmocks acontroller acontroller;     @injectmocks private aserviceimpl aserviceimpl;      @autowired webapplicationcontext webapplicationcontext;     @autowired private filterchainproxy springsecurityfilterchain;      @autowired     @injectmocks     private entitymanagerfactory entitymanagerfactory;       @before     public void setup() {         mockitoannotations.initmocks(this);         mockmvc = mockmvcbuilders.webappcontextsetup(webapplicationcontext)                                  .addfilter(springsecurityfilterchain)                                  .build();     }      @test     public void postatest() throws exception {          a.setdddd("xxx");          entitymanagerfactory entitymanagerfactory = webapplicationcontext.getbean(entitymanagerfactory.class);         entitymanager entitymanager = entitymanagerfactory.createentitymanager();         when(this.entitymanagerfactory.createentitymanager()).thenreturn(entitymanager);          when(aserviceimpl.createentity(isa(a.class))).thenreturn(a);          b.setcccc;         a.setmovietranslations(arrays.aslist(b));          when(aserviceimpl.createentity(isa(b.class))).thenreturn(a);          mockmvc.perform(post("/path")                .andexpect(status().isok())                .andreturn().getresponse().getcontentasstring();     } 

the createentitymethod:

public object createentity(t t) {     try {         entitymanager.persist(t);         return t;     } catch (illegalargumentexception | entityexistsexception | ... } 

the error log:

org.mockito.exceptions.misusing.missingmethodinvocationexception:  when() requires argument has 'a method call on mock'. example:     when(mock.getarticles()).thenreturn(articles);  also, error might show because: 1. stub either of: final/private/equals()/hashcode() methods.    methods *cannot* stubbed/verified.    mocking methods declared on non-public parent classes not supported. 2. inside when() don't call method on mock on other object.      @ com.x.server.controller.mcontrollertest.postatest(mcontrollertest.java:121) 

when don't inject mock object on entitymanager, got nullpointer exception persist method error log:

java.lang.nullpointerexception: null     @ com.x.server.serviceimpl.amanageserviceimpl.createentity(amanageserviceimpl.java:45)     @ com.eza.server.controller.mcontrollertest.postatest(mcontrollertest.java:123) 

can me?

cheers

solve it. removed line of code entitymanager interface , works.

public class mcontrollertest {      private mockmvc mockmvc;      @injectmocks a;     @injectmocks b b;     @injectmocks acontroller acontroller;      @autowired webapplicationcontext webapplicationcontext;     @autowired private filterchainproxy springsecurityfilterchain;      @before     public void setup() {         mockitoannotations.initmocks(this);         mockmvc = mockmvcbuilders.webappcontextsetup(webapplicationcontext)                                  .addfilter(springsecurityfilterchain)                                  .build();     }      @test     public void postmovietest() throws exception {          a.setdddd("xxx");         b.setcccc;         a.setmlist(arrays.aslist(b));           mockmvc.perform(post("/path")             .content(asjsonstring(a))             .contenttype(mediatype.application_json)             .accept(mediatype.application_json)             .andexpect(status().isok())             .andreturn().getresponse().getcontentasstring();     } 

asjsonstring me self write method convert object in json

cheers


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -