简体   繁体   中英

My rest services relies on an external service. How do I mock it to use on my cucumber tests?

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);
}

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"....)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM