#Testing ServerUtils SpringBoot
1 messages · Page 1 of 1 (latest)
For testing the most common is to use the JUnit testing-framework
If your methods which you want to test depend on other dependencies you should take a look at Mockito
Its better to view some free YouTube-course or other resources online. Explaining it all in that thread chat would be out of scope
yea alright i did it with mockito but my test returns null...
java ```
@Test
public void testGetEvents() {
// Mock the Client
Client mockClient = mock(Client.class);
// Mock the WebTarget and Response
WebTarget mockTarget = mock(WebTarget.class);
Builder mockBuilder = mock(Builder.class);
Response mockResponse = mock(Response.class);
// Mock the response data
List<Event> mockedEvents = List.of(new Event(/* event details */));
when(mockResponse.getStatus()).thenReturn(OK.getStatusCode());
when(mockResponse.readEntity(new GenericType<List<Event>>() {})).thenReturn(mockedEvents);
// Setup the mocks
when(mockClient.target(anyString())).thenReturn(mockTarget);
when(mockTarget.path(anyString())).thenReturn(mockTarget);
when(mockTarget.request(MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);
when(mockBuilder.accept(MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);
when(mockBuilder.get()).thenReturn(mockResponse);
// Call the method being tested
ServerUtils serverUtils = new ServerUtils(mockClient);
//serverUtils.addEvent(mockedEvents.get(0));
List<Event> result = serverUtils.getEvents();
// Verify the result
assertEquals(mockedEvents, result);
}
the result here is nul for some reason
Detected code, here are some useful tools:
@Test
public void testGetEvents() {
// Mock the Client
Client mockClient = mock(Client.class );
// Mock the WebTarget and Response
WebTarget mockTarget = mock(WebTarget.class );
Builder mockBuilder = mock(Builder.class );
Response mockResponse = mock(Response.class );
// Mock the response data
List<Event> mockedEvents = List.of(new Event(/* event details */
));
when(mockResponse.getStatus()).thenReturn(OK.getStatusCode());
when(mockResponse.readEntity(new GenericType<List<Event>>() {
}
)).thenReturn(mockedEvents);
// Setup the mocks
when(mockClient.target(anyString())).thenReturn(mockTarget);
when(mockTarget.path(anyString())).thenReturn(mockTarget);
when(mockTarget.request(MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);
when(mockBuilder.accept(MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);
when(mockBuilder.get()).thenReturn(mockResponse);
// Call the method being tested
ServerUtils serverUtils = new ServerUtils(mockClient);
//serverUtils.addEvent(mockedEvents.get(0));
List<Event> result = serverUtils.getEvents();
// Verify the result
assertEquals(mockedEvents, result);
}
java ```
public List<Event> getEvents() {
return client //
.target(SERVER).path("api/events") //
.request(APPLICATION_JSON) //
.accept(APPLICATION_JSON) //
.get(new GenericType<List<Event>>() {});
}
and this is the method im testing
Detected code, here are some useful tools:
public List<Event> getEvents() {
return client//
.target(SERVER).path("api/events") //
.request(APPLICATION_JSON) //
.accept(APPLICATION_JSON) //
.get(new GenericType<List<Event>>() {
}
);
}