这是实际代码
ratenegotiationcontroller.java
@GetMapping(value = "/rate-negotiation/{uniqueId}", produces = {APPLICATION_JSON_VALUE})
public ResponseEntity<RateNegotiation> rateNegotiationByUniqueId(@PathVariable(name = "uniqueId") final String uniqueId) {
final RateNegotiation rateNegotiation =
rateNegotiationService.retrieveRateNegotiationsByUniqueId(uniqueId);
final Optional<String> courierID = validationUtils.getCourierIDFromToken();
if (courierID.isPresent()) {
if (!courierID.get().equals(rateNegotiation.getCourierId())) {
return ResponseEntity.notFound().build();
}
log.info("RateNegotiationController, rateNegotiationByUniqueId {} ", rateNegotiation);
return ResponseEntity.ok(rateNegotiation);
}
throw new CourierIdNotFoundException(COURIER_ID_NOT_FOUND);
}
ValidationUtils.java
public Optional<String> getCourierIDFromToken() {
if (appConfigBean.isSecurityEnabled()) {
return Optional.of(requestPayloadValidator.getCourierIDFromToken());
}
return Optional.empty();
}
我正在为这个写测试用例。。
@MockBean
private ValidationUtils validationUtils;
@MockBean
private AppConfigBean appConfigBean;
@MockBean
private RequestPayloadValidator requestPayloadValidator;
@测试公共void shouldretrieveratenegotiationdetailsbyuniqueid(){
when(validationUtils.getCourierIDFromToken()).thenReturn(Optional.of("123456"));
when(appConfigBean.isSecurityEnabled()).thenReturn(true);
when(requestPayloadValidator.getCourierIDFromToken()).thenReturn("123456");
rateNegotiationServiceWireMockRule.stubFor(WireMock.get(urlEqualTo(RETRIEVE_RATE_NEGOTIATION_BY_UNIQUE_ID_PATH))
.willReturn(aResponse()
.withHeader(CONTENT_TYPE, APPLICATION_JSON_CHARSET)
.withBodyFile("RateNegotiationByUniqueId.json")
.withStatus(200)
)
);
given()
.port(port)
.when()
.header(CONTENT_TYPE, APPLICATION_JSON_CHARSET)
.get(RETRIEVE_RATE_NEGOTIATION_BY_UNIQUE_ID_URL)
.then()
.assertThat()
.statusCode(200);
}
但它仍然不是wokring,并且显示错误,courieridnotfoundexception:courier id not found i have mock the method validationutils.getcourieridfromtoken(),但它仍然不是wokring有人能帮忙吗?
1条答案
按热度按时间ugmeyewa1#
demoappcontroller.java文件
demoapprequest.java文件
demoappcontrollertest.java文件
testserviceapiintegrationtest.java测试服务
这只是一个单元测试的模板,您可以在项目中遵循并实现它。