Junit 测试用例未按预期工作

问题描述 投票:0回答:1

尝试了几乎所有内容,响应中仍然为空

下面是测试用例

@Test
public void testValidateMemberData_Success() throws Exception {
    MemberValidationRequest memberRequest = new MemberValidationRequest();
    memberRequest.setCorelationIdentifier("123");

    MemberDataValidationResponse mockResponse = new MemberDataValidationResponse();
    mockResponse.setStatus("200");

    HttpHeaders headers = SelfServiceValidation.populateTokenHeaders();
    HttpEntity<String> httpEntity = new HttpEntity<>(new JSONObject(memberRequest).toString(), headers);
    
    when(SelfServiceValidation.getRestTemplate()).thenReturn(restTemplate);
    
    when(restTemplate.exchange(any(), eq(HttpMethod.POST), any(HttpEntity.class), eq(MemberDataValidationResponse.class)))
            .thenReturn(ResponseEntity.ok(mockResponse));
    MemberDataValidationResponse response = new MemberDataValidationResponse();
    
     try (MockedStatic<SelfServiceDaoHelper> mockStatic = mockStatic(SelfServiceDaoHelper.class)) {
         
         mockStatic.when(() ->SelfServiceDaoHelper.auditApiRequestData(any(MapSqlParameterSource.class))).thenReturn(1);
                 
     assertEquals(1, SelfServiceDaoHelper.auditApiRequestData(new MapSqlParameterSource()));
     MemberDataValidationResponse result = SelfServiceValidation.validateMemberData(memberRequest);
     response = result;
     
     };
    
    assertNotNull(response);
    assertEquals("200", response.getStatus());
}

以下是它的方法。

public static MemberDataValidationResponse validateMemberData(MemberValidationRequest memberValidationRequest)
        throws Exception {
    MemberDataValidationResponse memberValidateResponse = new MemberDataValidationResponse();
    try {
        if (isSelfServicePortalServerAvailable(selfServicePortalURL)) {
            String corelationId = UUID.randomUUID().toString();
            InetAddress ipAddress = InetAddress.getLocalHost();
            String spanId = corelationId + SelfServicePortalConstants.MEMBER_VALIDATION_CODE;
            HttpHeaders headers = populateTokenHeaders();
            RestTemplate restTemplate = getRestTemplate();
            String validateMemberAPI = selfServicePortalURL + Endpoints.MEMBER_VALIDATION;
            log.info("Self Service Portal API URL :{}", validateMemberAPI);
            JSONObject jsonObj = new JSONObject(memberValidationRequest);
            String requestBody = jsonObj.toString();
            HttpEntity<String> request = new HttpEntity<>(requestBody, headers);
            processRequestAudit(memberValidationRequest, corelationId, spanId, ipAddress.toString(),
                    "Member Validation", "Claim Submission");
            UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(validateMemberAPI);
            ResponseEntity<MemberDataValidationResponse> response = restTemplate.exchange(
                    builder.buildAndExpand().toUri(), HttpMethod.POST, request, MemberDataValidationResponse.class);
            memberValidateResponse = (MemberDataValidationResponse) response.getBody();
            processResponseAudit(memberValidateResponse, "", spanId, corelationId);
        } else {
            log.info(SELF_SERVICE_PORTAL_UNAVAILABLE_MESSAGE);
        }
    } catch (Exception cause) {
        throw new RuntimeException(cause);
    }
    return memberValidateResponse;
}


public static RestTemplate getRestTemplate (){
    RestTemplate restTemplate = new RestTemplate();
    return restTemplate;
}
testing junit mockito powermockito
1个回答
0
投票
  • 您只需分配响应,但切勿在行中使用它
    response = result;
  • 修复:
        MemberDataValidationResponse response = new MemberDataValidationResponse();

        try (MockedStatic<SelfServiceDaoHelper> mockStatic = mockStatic(SelfServiceDaoHelper.class)) {

            mockStatic.when(() ->SelfServiceDaoHelper.auditApiRequestData(any(MapSqlParameterSource.class))).thenReturn(1);

            assertEquals(1, SelfServiceDaoHelper.auditApiRequestData(new MapSqlParameterSource()));
            response = SelfServiceValidation.validateMemberData(memberRequest);

        };

        assertNotNull(response);
        assertEquals("200", response.getStatus());
© www.soinside.com 2019 - 2024. All rights reserved.