java - 模拟方法不起作用
问题描述
这是实际的代码
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;
@Test public 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 我已经模拟了方法 validationUtils.getCourierIDFromToken() 但它仍然不是 wokring 任何人都可以帮忙吗?
解决方案
DemoAppController.java
package com.application.packagename.controller;
@RestController
@Api(value="demoappcontroller", description="Application configuration")
@RequestMapping("app")
@ApiIgnore
public class DemoAppController {
@Autowired
SomeService service;
@ApiOperation(value = "demo app config", response = DemoReponse.class)
@RequestMapping(value="/v1/getDemoAppInfo", produces = "application/json", method= RequestMethod.GET)
public ResponseEntity getDesc(@Valid DemoAppRequest demoAppRequest) {
DemoReponse response = service.getDemoAppInfo(demoAppRequest.getVarNameOne(),
demoAppRequest.getEnvType());
return new ResponseEntity(response, HttpStatus.OK);
}
}
DemoAppRequest.java
package com.application.packagename.model;
@Data
@Component("demoapprequestVo")
@ApiModel("demoapprequestVo")
public class DemoAppRequest {
@ApiModelProperty(example = "value1")
public String varNameOne;
@ApiModelProperty(example = "value2")
public String varNameTwo;
}
DemoAppControllerTest.java
public class DemoAppControllerTest extends TestServiceApiIntegerationTest {
private MultiValueMap<String, String> requestParams;
private URI url;
@BeforeEach
void init() {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("varNameOne", "value 1");
params.add("varNameTwo", "value 2");
requestParams = params;
url = URI.create("/app/v1/getDemoAppInfo/");
}
@Test
public void testGetDesc() throws Exception {
mockMvc.perform(get(url)
.params(requestParams))
.andExpect(status().isOk());
}
}
TestServiceApiIntegrationTest.java
@SpringBootTest
@AutoConfigureMockMvc
public class TestServiceApiIntegerationTest {
@Autowired
protected MockMvc mockMvc;
}
这只是一个单元测试的模板,你可以在你的项目中遵循并实现它。
推荐阅读
- java - 如何安装hadoop纱线?
- r - R tidy 按组添加摘要
- sql - 为表中的元组选择最大值
- javascript - 对要上传到服务器的文件进行防病毒扫描 - Node.js
- java - Spring Data JPA:数据类型 varchar 和 varbinary 不兼容在使用 concat() 方法时发生 add 运算符异常
- ios - 支持滑动以关闭工作表中呈现的 UIViewControllerRepresentable
- node.js - 在 Nodejs 中将 HTML 转换为 PDF 缓冲区
- r - 检查一个数据帧的值是否按确切顺序存在于另一个数据帧中
- python - 查找 .stl(CAD 绘图)文件和 3D 图(matplotlib)之间的交点?
- excel - VBA FormulaArray 不适用于替换函数