Skip to content
Advertisement

Should such a class be tested? (aggregation of already tested functions)

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);
}

Advertisement

Answer

Yes. You definetely have to test A.createSession() method:

  1. Nobody can guarantee you that code will remain the same. If you or other developer will have to refactor or change your code, you will able to do it safely, because you will check correctness by test, otherwise developer can break something unintentionally.
  2. When you write tests, you actually think about a good design, without tests, you will end badly. You mention it yourself:

which makes me wonder about the method design

As for efforts for a testing – it means you have the bad design in your test classes and you have to refactor and keep them (tests) clear like the main code.

You can read more why you have to test and why it so important in that question.

Advertisement