spring - mockito spy object with annotated method -


if add annotation on method of mocked objet, mock disabled:

@service public class purchaseservice extends abstractservice {     @autowired     private checkoutservice checkoutservice;      @eventannotation(eventtypeenum.prc_product)     public checkoutresponse buy(string productid) {         return checkoutservice.buy(productid);     } }   @service public class checkoutservice extends abstractservice {     public checkoutresponse buy(executioncontext context, string productid) {         ..... } 

and test:

@autowired @spy private userservice userservice;  ...  @test public void should_buy() {     // given     string productid = "productid";     loyalty loyalty = new loyalty(15, new grade("1", "label", 10));      when(catalogclient.findpromoproductformember(default_tenant, productid, userid)).thenreturn(promoproduct);     when(userservice.getuserloyalty(userid)).thenreturn(loyalty);      // when     checkoutresponse result = purchaseservice.buy(productid); // <---      //     assertthat(response).isnotnull();     assertthat(result).issameas(response); } 

if change purchaseservice.buy checkoutresponse result = checkoutservice.buy, works

have solution or around?

real answer

don't inject userservice test. mock it. unit test, not integration test. don't need call real class testing.

irrational answer

don't use spy annotation. declare @before method , spy injected object therein. example,

import static org.mockito.mockito.spy;  @autowired private smashy smasher;  @before public void pretestsetup() {     // love unnecessary work! (be sure include comment).     smasher = spy(smasher); } 

edit if either 1 or 2 in comment below true, believe may need either mock smashy interface or this:

@before public void pretestsetup() {     smasher = mock(smashy.class); // replace injected mock.      doreturn(...; // setup mocking behavior. } 

this is; however, variation of "real answer" above.

consider well:

@mock private smashy mocksmashy;  @before public void pretestsetup() {     mockitoannotations.initmocks(this);      doreturn(...; // setup mocking behavior. } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -