java - Mock 3rd Party API calls in Spring MVC -


so current situation such. have build app , running tests on controller. tests hit actual 3rd party api , jackson binding on result map in pojo objects.

i kind of unsure how mock whole thing without me ending population pojo manually . looking take mock json response , bind pojo , can verify matches data on mock json.

here sample of third part calling api

/**  * makes api call , stores result in pojo  * should gracefully handle errors  * @return  */ public 3rdpartysearchresult searchapicall(){     if(productquery==null||productquery.isempty() || productquery.trim().isempty()){         throw new nullpointerexception("query string cannot empty");     }     resttemplate resttemplate = new resttemplate();     walmartsearchresult wsr = resttemplate.getforobject(3rdpartyapidetails.searchurl, 3rdpartypojo.class,3rdpartyapidetails.apikey,productquery);     return wsr; } 

ii somehow need mock resttemplate.getforobject point mock json file.

the following example test shows 1 way it, using jmockit mocking library:

@test public void exampletestforsearchapicall(@mocked resttemplate rest) {     searchapi searchapi = new searchapi(...productquery...);      3rdpartysearchresult result = searchapi.searchapicall();      assertnotnull(result);      // verify expected call resttemplate:     new verifications() {{ rest.getforobject(...argument values and/or matchers...); }}; }