cancel
Showing results for 
Search instead for 
Did you mean: 

Mocking RestTemplate in listener in unit test

activitinike
Champ in-the-making
Champ in-the-making
Hello,

We have RestTemplate class usage to make REST API calls, defined in Service tasks and listeners.

For unit testing, I would like to mock RestTemplate while running end to end process test.

I tried following, but it works only if I test listener class or JavaDelegate class directly, but not while executing process instance using ActivitiRule and InMemory config.


//Mock rest api calls
      RestTemplate restTemplate = new RestTemplate();
      MockRestServiceServer mockRestServer = MockRestServiceServer.createServer(restTemplate);
      
      //json response
      String responseString200_task = "{MyJsonStructure}";
      
      mockRestServer.expect(requestTo("RESTURLwkfl"))
               .andExpect(method(HttpMethod.POST))
               .andRespond(withStatus(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(responseString200_task));



When I run process in unit test using:

activitiRule.getRuntimeService().startProcessInstanceByKey("mykey");

It execute the process instance, but it still makes actual call to REST API server, rather than mocking the call.

Is there a way to make it work?
Similar situation for and DAO dependency in Listeners or ServiceTask delegate.

Thanks for any help.
1 REPLY 1

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

if you want to completely omit rest service call, you can hook into process parsing and replace the rest calls with mocks.
Or you can have a look on org.activiti.engine.test.bpmn.servicetask.WebServiceTaskTest.

regards
Martin
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.