Stubbing Keycloak SimpleHttp.Response.asjson (Klasse Typ)Java

Java-Forum
Guest
 Stubbing Keycloak SimpleHttp.Response.asjson (Klasse Typ)

Post by Guest »

Ich habe Probleme, die methode von SimpleHttp.Response (Klasse Typ) zu stürzen. Hier ist der DOC: KeyCloak SimpleHttp < /p>
Hier ist der Beispielcode, den ich stören möchte: < /p>

Code: Select all

SampleResponse sampleResp = response.asJson(SampleResponse.class);
< /code>
Hier ist die Methode, die ich testen möchte: < /p>
    public SampleResponse getCustomer(String clientID) throws ResponseFailureException {

String url = String.format("%s/customer", this.baseUrl);
Instant start = Instant.now();
try {
// Build request
//
SimpleHttp req = SimpleHttp
.doGet(url, httpClient)
.param("client_id", clientID)
.json(null);

// Fire the request
//
SimpleHttp.Response response = req.asResponse();
if (response.getStatus() != HttpStatus.SC_OK) {
throw new ResponseFailureException("Fail");
}

// Parse response
// Target code to stub
SampleResponse sampleResp = response.asJson(SampleResponse.class);

return sampleResp;
} catch (Exception e) {
throw new ResponseFailureException("Fail", e);
}
}
< /code>
Hier ist der Test: < /p>
class ClientTest {

@Mock
private KeycloakSession keycloakSession;

@Mock
private HttpClientProvider httpClientProvider;

@Mock
private CloseableHttpClient httpClient;

@Mock
private SimpleHttp simpleHttp;

@Mock
private SimpleHttp.Response response;

private Client testClient;

@BeforeEach
void setUp() {
when(keycloakSession.getProvider(HttpClientProvider.class)).thenReturn(httpClientProvider);
when(httpClientProvider.getHttpClient()).thenReturn(httpClient);
testClient = new Client(keycloakSession, "https://api.example.com", "BASIC", "user", "pass");
}

@Test
void getCustomer_shouldReturnResponse_whenValidInputsProvided() throws Exception {
// Mock `SimpleHttp.doGet()` correctly with KeycloakSession

try (MockedStatic mockedSimpleHttp = mockStatic(SimpleHttp.class)) {
mockedSimpleHttp.when(() -> SimpleHttp.doGet(eq("https://api.example.com/customer"), eq(httpClient)))
.thenReturn(simpleHttp);

// Mock behavior of the SimpleHttp instance
when(simpleHttp.header(anyString(), anyString())).thenReturn(simpleHttp);
when(simpleHttp.param(anyString(), anyString())).thenReturn(simpleHttp);
when(simpleHttp.json(null)).thenReturn(simpleHttp);
when(simpleHttp.asResponse()).thenReturn(response);

String jsonResponse = "{\"customerInfo\": {\"name\": \"testName\", \"permissions\": \"admin\"}}";

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonResponse);

// Mock the response
when(response.getStatus()).thenReturn(200);
when(response.asJson()).thenReturn(node);
SampleResponse sampleResponse = new SampleResponse();

// Stub the asJson method
// Errors in this line of code
when(response.asJson(any(SampleResponse.class))).thenReturn(sampleResponse);

// Call the actual method
SampleResponse result = testClient.getCustomer("client123");

// Verify the expected result
assertNotNull(result);
}
}
}
< /code>
Ich habe versucht, ChatGPT nach Lösungen zu fragen, die gelieferte Lösung jedoch immer noch fehlschlägt.  Hier ist eine Beispiellösung: < /p>
SampleResponse sampleResponse = new SampleResponse();
when(response.asJson(SampleResponse.class)).thenReturn(sampleResponse);
< /code>
Es gibt diesen Fehler zurück: < /p>
org.mockito.exceptions.misusing.NotAMockException: Argument passed to Mockito.mockingDetails() should be a mock, but is an instance of class java.lang.Class!
< /code>
Ich habe auch diese Lösung ausprobiert: < /p>
when(response.asJson(any(SampleResponse.class))).thenReturn(sampleResponse);
< /code>
Es schlägt immer noch fehl und gibt diesen Fehler zurück: < /p>
no suitable method found for asJson(sampleResponse)
method org.keycloak.broker.provider.util.SimpleHttp.Response.asJson(java.lang.Class) is not applicable
(inference variable T has incompatible bounds
equality constraints: sampleResponse
lower bounds: java.lang.Object,java.lang.Class)
method org.keycloak.broker.provider.util.SimpleHttp.Response.asJson(com.fasterxml.jackson.core.type.TypeReference) is not applicable
(inference variable T has incompatible bounds
equality constraints: sampleResponse
lower bounds: java.lang.Object,com.fasterxml.jackson.core.type.TypeReference

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post