RestTemplate verspottet nicht in jungitJava

Java-Forum
Anonymous
 RestTemplate verspottet nicht in jungit

Post by Anonymous »

Ich habe diese Methode und probiert alles aus, aber das Verspotten funktioniert nicht wie erwartet < /p>
@Override
public ClaimSubmissionResponse generateJsonFileProfessional(String claimType, String correlationId,
String jsonContent, String authToken) {
ClaimSubmissionResponse claimSubmissionResponse = new ClaimSubmissionResponse();
try {
if (isSelfServicePortalServerAvailable(selfServicePortalURL)) {
HttpHeaders headers = populateTokenHeaders(authToken);
String corelationId = UUID.randomUUID().toString();
InetAddress ipAddress = InetAddress.getLocalHost();
HttpEntity request = new HttpEntity(jsonContent, headers);
String claimSubmissionAPI = selfServicePortalURL + Endpoints.SUBMIT_CLAIM_PROFESSIONAL;
processRequestAudit(jsonContent, corelationId, corelationId, ipAddress.toString(), "Generate Json File",
"Claim Submission");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(claimSubmissionAPI)
.queryParam("claimType", claimType).queryParam("correlationId", correlationId);
ResponseEntity response = restTemplate.exchange(
builder.buildAndExpand().toUri(), HttpMethod.POST, request, ClaimSubmissionResponse.class);
claimSubmissionResponse = response.getBody();
processResponseAudit(claimSubmissionResponse, "", corelationId, corelationId);
} else {
log.info(SELF_SERVICE_PORTAL_UNAVAILABLE_MESSAGE);
}
} catch (Exception cause) {
log.error("The Error While Executing the API Call :: {} ", cause.getMessage());
}
return claimSubmissionResponse;
}
< /code>
JUNIT -Testfall: < /p>
@Test
public void testGenerateJsonFileProfessional_Success() throws Exception {
// Mocking external methods and variables
String claimType = "type1";
String correlationId = UUID.randomUUID().toString();
String jsonContent = "{}";
String authToken = "token123";
String corelationId = UUID.randomUUID().toString();
InetAddress ipAddress = InetAddress.getLocalHost();
// Mocking isSelfServicePortalServerAvailable
when(selfServiceCoreClaimImpl.isSelfServicePortalServerAvailable(selfServicePortalURL)).thenReturn(true);
// Mocking populateTokenHeaders
when(selfServiceCoreClaimImpl.populateTokenHeaders(authToken)).thenReturn(httpHeaders);
// Mocking RestTemplate response
ClaimSubmissionResponse expectedResponse = new ClaimSubmissionResponse();
ResponseEntity mockResponse = new ResponseEntity(expectedResponse, HttpStatus.OK);
// UriComponentsBuilder mock setup
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(selfServicePortalURL)
.queryParam("claimType", claimType)
.queryParam("correlationId", correlationId);
when(restTemplateTest.exchange(
builder.buildAndExpand().toUri(), HttpMethod.POST, new HttpEntity(jsonContent, httpHeaders), ClaimSubmissionResponse.class))
.thenReturn(mockResponse);
// Call the method under test
ClaimSubmissionResponse actualResponse = selfServiceCoreClaimImpl.generateJsonFileProfessional(claimType, correlationId, jsonContent, authToken);
// Assertions
assertNotNull(actualResponse);
assertEquals(expectedResponse, actualResponse);
// Verify RestTemplate was called
verify(restTemplateTest).exchange(
builder.buildAndExpand().toUri(), HttpMethod.POST, new HttpEntity(jsonContent, httpHeaders), ClaimSubmissionResponse.class);
}
}
< /code>
Erhalten Sie diesen Fehler < /p>

org.mockito.Exceptions.misusing.missingMethodinVocationException:
Wenn () erfordert eine Argumentation, die 'eine Methode auf einem Mock'. /> whten(mock.getArticles()). ThenReturn(Articles);
Auch dieser Fehler kann angezeigt werden, weil:
  • Sie haben eine von: Final/private/equals ()/Hashcode () -Methoden. Nicht öffentliche übergeordnete Klassen werden nicht unterstützt. com.acentra.ssp.impl.elfserviceCoreClaimimPltest.TestGenerateJsonFilepressional_Success (selfServiceCoreClaimimPltest.java:170)
    bei java.base /java.rang.reflect.method.invoke (methode. method.javarang.meth.meth.Meth.invoke (methode java.base/java.util.arraylist.foreach (arraylist.java:1511)
    at java.base/java.util.arrayList.foreach (ArrayList.java:1511)
    @Test
    public void testGenerateJsonFile_Success() {
    ClaimSubmissionResponse mockResponse = new ClaimSubmissionResponse();
    ResponseEntity responseEntity = mock(ResponseEntity.class);
    when(responseEntity.getBody()).thenReturn(mockResponse);
    when(restTemplate.exchange(any(), eq(HttpMethod.POST), any(HttpEntity.class), eq(ClaimSubmissionResponse.class))).thenReturn(responseEntity);

    ClaimSubmissionResponse response = selfServiceCoreClaimImpl.generateJsonFileProfessional("claimType", "correlationId", "jsonContent", "testdata");
    assertNotNull(response);
    }
    < /code>
    Dann wird hier
    die Antwort null < /p>
    ResponseEntity response = restTemplate.exchange(
    builder.buildAndExpand().toUri(), HttpMethod.POST, request, ClaimSubmissionResponse.class);
    claimSubmissionResponse = response.getBody();
    < /code>
    Fehler ist < /p>

    org.mockito.Exceptions.misusing.unnessyStubbingException:
    Unnötige Stubbinge erkannt. Code): < /p>

    -> at> com.impl.selfServiceCoreClaimimPlTest.TestGenerateJsonFile_Success (SelfServiceCoreClaimPlTest.java:147) < /li>
    ->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->-> ter ter com.impl.selfServiceCoreClaimImPltest.TestGenerateJsonFile_Success (selfserviceCoreClaimimPltest.java:148)
    Bitte entfernen Sie unnötige Stubbings oder verwenden Sie "lenient" Strenge. Weitere Informationen: Javadoc für unnötige Klasse. java.base/java.util.arrayList.foreach (arrayList.java:1511)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post