有人能告诉我一个例子,如何使用Spock测试RestTemplate。 我的课程看起来像这样:
@Service
public class SomeService {
@Autowired
private EndpointUrlProvider endpointUrlProvider;
private RestTemplate restTemplate = new RestTemplate();
public SomeResponse doSomePostRequest(HttpEntity<?> httpEntity) throws Exception {
ResponseEntity<SomeResponse> response;
try{
response = restTemplate.postForEntity(endpointUrlProvider.getSomeUrl(),httpEntity,SomeResponse.class);
} catch (Exception e){
throw new Exception("Exception occured during post for:" + httpEntity.getBody().getClass().getSimpleName() + " Cause: " + e.getMessage());
}
if(response.getStatusCode() == HttpStatus.OK){
return response.getBody();
}
throw new Exception("Error during " + response.getBody().getClass().getSimpleName() + "Http status is diffrent than 200: " + response.getBody().toString());
}
}
试验:
class SomeTest extends Specification {
RestTemplate restTemplate = Mock {
postForEntity(_, _, SomeResponse.class) >> new ResponseEntity(new SomeResponse(), HttpStatus.OK)
}
@Autowired
SomeService someService
def "someTest"() {
when:
SomeResponse someResponse = someService.doSomePostRequest(new HttpEntity<>(new SomeBody(), new HttpHeaders()))
then:
someResponse == new SomeResponse()
}
}
主要问题是模拟RestTemplate的行为,我正在寻找解决方案如何以适当的方式做到这一点。我不使用弹簧靴。
答案 0 :(得分:2)
情况如下:
您在服务类中创建新的RestTemplate
。
private RestTemplate restTemplate = new RestTemplate();
然后在测试中创建模拟并调用您的服务方法:
RestTemplate restTemplate = Mock {
postForEntity(_, _, SomeResponse.class) >> new ResponseEntity(new SomeResponse(), HttpStatus.OK)
}
....
someService.doSomePostRequest
但你的服务仍然有通常的休息模板。你应该注入模拟对象。我建议你通过构造函数来做。所以将代码更改为:
@Service
public class SomeService {
private EndpointUrlProvider endpointUrlProvider;
private RestTemplate restTemplate;
@Autowired
public SomeService(EndpointUrlProvider endpointUrlProvider, RestTemplate restTemplate){
this.endpointUrlProvider = endpointUrlProvider;
this.restTemplate = restTemplate;
}
你的测试将是:
class SomeTest extends Specification {
RestTemplate restTemplate = Mock {
postForEntity(_, _, SomeResponse.class) >> new ResponseEntity(new SomeResponse(), HttpStatus.OK)
}
SomeService someService = new SomeService ( null, restTemplate);
def "someTest"() {
when:
SomeResponse someResponse = someService.doSomePostRequest(new HttpEntity<>(new SomeBody(), new HttpHeaders()))
then:
someResponse == new SomeResponse()
}
}
现在,您的服务对象将在注入的MOCK上调用方法,而不是通常的RestTemplate
P.S。
构造函数注入在春天被认为是一种很好的做法。
最好创建一个RestTemplate bean并将其注入到任何地方,而不是在所有服务中创建新对象。