I have the following code in Class A where both requestService.createGetSessionRequest()
and httpService.sendAsyncAndReceive()
are tested already in other unit tests:
public GetSessionResponse createSession(String endpointBaseUrl, String endpointId, String salt, String endpointSecret) throws JsonProcessingException, ExecutionException, InterruptedException, HttpException {
final HttpRequest request = requestService.createGetSessionRequest(endpointBaseUrl,endpointId,salt,endpointSecret);
final GetSessionResponse response = httpService.sendAsyncAndReceive(request, GetSessionResponse.class);
return response;
}
I am wondering, should I even create tests for A.createSession()
?
Basically it seems that it could be useful as a developer might accidentally mix up the parameters when calling requestService.createGetSessionRequest()
in A.createSession()
's implementation as they're all strings.
On the other side, the efforts for a test seem pretty high, which makes me wonder about the method design (just aggregating multiple calls into one single method so I have a cleaner interface for users)
A sample test case looks like this:
void createSessionSuccessfullyTest() throws HttpException, ExecutionException, InterruptedException, JsonProcessingException {
String endpointBaseUrl = "http://te.st";
String endpointId = "abc";
String salt = "salt";
String endpointSecretHash = "hash";
HttpRequest request = requestService.createGetSessionRequest(endpointBaseUrl,endpointId,salt,endpointSecretHash);
String endpoint_session_id = "0TbKHn9MsZKJYhfQ0FZ0W2y0RHVwxTOY";
GetSessionResponse expected = new GetSessionResponse();
expected.setEndpointSessionId(endpoint_session_id);
HttpService httpService = mock(HttpService.class);
when(httpService.sendAsyncAndReceive(request, GetSessionResponse.class)).thenReturn(expected);
AuthenticationFlow authenticationFlow = new AuthenticationFlowImpl(requestService,httpService);
GetSessionResponse actual = authenticationFlow.createSession(endpointBaseUrl,endpointId,salt,endpointSecretHash);
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
}