I made some cucumber tests (given, when, then integration tests) in spring boot maven, and I was able to test my endpoints using TestRestTemplate, but only if the external service is running.
The external service job is to check some fields before I save the information to the database.
When running the application normally the external service is called by FeignClient.
Is it possible to mock the external service while using TestRestTemplate to test the endpoints?
EDIT:
Sample mockup of the structure
Controller.java
@Autowired
EmployeeSvc employeeSvc;
@PostMapping(/save)
public ResponseEntity<String> saveEmployee(@RequestBody EmployeeDTO employeeDTO) {
employeeSvc.save(employeeDTO);
}
EmployeeSvc.java
@Autowired
ExternalSvcClient externalSvcClient; //External service is called by feignclient here.
@Autowired
EmployeeRepository employeeRepository;
public void save(EmployeeDTO employeeDTO) {
externalSvcClient.check(employeeDTO);
...some other code
Employee employee = dtoTransformer.transoform(employeeDTO);
employeeRepository.save(employee);
}
CodePudding user response:
OK I managed to make my tests using wiremock by following this .
For stubs be sure not to include the parameters in the path:
if your request url looks like this
myrequest/?value1=someValue
make it like this in the stubs and append the parameters on the rest template or else it won't work
myrequest/
restTemplate.getForEntity(url "value1=someValue"....)