请找到以下提到的代码:
我需要帮忙为Mockito
编写ResponseEntity
条件:
if(isObjectPresent(ePartnerRestRequestDTO))
{
ePartnerRestRequestDTO.setLoggerId(loggerId);
String url = new StringBuilder(EPARTNER_MICRO_SERVICE_URL).append(service).toString();
RequestEntity<String> requestEntity = new RequestEntity<String>(prePreocess(ePartnerRestRequestDTO), HttpMethod.POST, new URI(url));
Instant before = Instant.now();
ResponseEntity<String> response = new RestTemplate().exchange(requestEntity, String.class);
logger.info("ePartner service " + url + " - duration (milliseconds) :: " + Duration.between(before, Instant.now()).toMillis());
if(isObjectPresent(response) && isObjectPresent(response.getStatusCode()) && HttpStatus.OK == response.getStatusCode()) {
ePartnerRestResponseDTO = postProcess(JWSResponseUtil.verifyJWSSignatureAndExtractPayload(response.getBody()));
if(isObjectPresent(ePartnerRestResponseDTO) && isValuePresent(ePartnerRestResponseDTO.getResponseCode())) {
if(SUCCESS_CODE.equals(ePartnerRestResponseDTO.getResponseCode())) {
status = true;
}
}
}
}
答案 0 :(得分:0)
您无法使用香草Mockito模拟创建新对象。
1):我建议将对象的创建移至包级方法:
RequestEntity<String> getRequestEntity(/* params */){
return new RequestEntity<String>(prePreocess(ePartnerRestRequestDTO),
HttpMethod.POST, new URI(url))
}
2),然后监视被测类
@Spy
private ClassUnderTest classUnderTestSpy;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
doReturn(myPreparedRequestEntity).when(classUnderTestSpy).getRequestEntity(/* params*/);
}