java - Gradld Build - Use Mock Objects Created by Mockito for running unit tests -


i have spring boot application connects external webservice. project built using gradle. mocking external call in program. when run junit test in sts , test running successfully. when "gradle build", test failing. when looked logs , think failing because test hitting actual service instead of return mocked object. there need in order gradle build pick mock objects generated mockito part of tests?

@runwith(springjunit4classrunner.class) @springapplicationconfiguration(classes = proxyapplication.class) @webappconfiguration public class applicationwrappertest {      @mock     private soapclient soapclient;      @injectmocks     @autowired     private applicationwrapper applicationwrapper;      @before     public void init() {         mockitoannotations.initmocks(this);     }      @test     public void testdatesinapplication() throws exception{       //return mock object (webservice response) when soapclient called     } 

applicationwrapper code

@component public class applicationwrapper {      @resource(name = "factory.soapclient")     private soapclientfactory soapclientfactory;      @autowired     private jsonutil jsonutil;      @autowired     private domainutil domainutil;      private static final string clientid = "soapclient";      public string execute(string request, string apiname){         object req = domainutil.createrequest(request, apiname);         object jaxbresponse = this.soapclientfactory.getclient(clientid).marshalsendandreceive(req);          object response = domainutil.createresponse(jaxbresponse, apiname);         return jsonutil.tojsonstring(response) ;     }     } 

yes, hitting actual service because using beans plugged through proxyapplication.class. problem mixing 2 test approaches: mockito , spring.

to avoid these have several choices:

  1. run tests without springjunit4classrunner.class , spring configurations. remove @autowired. use mockitojunitrunner.class. true unit test

    @runwith(mockitojunitrunner.class) public class applicationwrappertest {  @mock private soapclientfactory soapclientfactory;  @mock private soapclient soapclient;  @injectmocks private applicationwrapper applicationwrapper;  @before public void init() {     mockitoannotations.initmocks(this); }  @test public void testdatesinapplication() throws exception{   when(soapclientfactory.getclient(eq("yourclientid"))).thenreturn(soapclient);   //return mock object (webservice response) when soapclient called } 
  2. create spring test configuration class spring configuration soapclient configured mock.


Comments