java模拟方法不是wokring

tvokkenx  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(200)

这是实际代码
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有人能帮忙吗?

ugmeyewa

ugmeyewa1#

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

这只是一个单元测试的模板,您可以在项目中遵循并实现它。

相关问题