0

As mentioned in title, I don't understand how I supposed to cover by tests code which is just wrap http api.

I guess I can write only unit tests, because wrapped service is paid. Integration tests in my opinion isn't required because goal is just provide strict structure of input and return raw output.

Am I right in my thoughts? If so can you please advice me how unit tests supposed to look like, because something like just verifying RestTemplate to be called feels useless.

Example:

public class TsMessageServiceImpl implements TsMessageService {
    private final RestTemplate restTemplate;

    @Override
    public Optional<TsResponse> send(TsSmsMessage smsMessage) {
        return this.send(new TsHybridMessageImpl(smsMessage));
    }

    @Override
    public Optional<TsResponse> send(TsViberMessage viberMessage) {
        return this.send(new TsHybridMessageImpl(viberMessage));
    }

    @Override
    @SneakyThrows
    public Optional<TsResponse> send(TsHybridMessage hybridMessage) {
        String url = TsEndpoint.MESSAGE_SEND.value();
        HttpEntity<TsHybridMessage> body = new HttpEntity<>(hybridMessage);
        Class<TsResponse> responseType = TsResponse.class;
        log.debug(new ObjectMapper().writeValueAsString(hybridMessage));
        ResponseEntity<TsResponse> response = restTemplate.postForEntity(url, body, responseType);
        return Optional.ofNullable(response.getBody());
    }
}
0

So the code actually does several things

  • http communication
  • deserialising messages
  • logging etc

Any of which can go wrong.

You are right in thinking that a unit test which mocks these things is useless, I would test with integration rather than unit tests if you also run the server.

But if a unit test is required you could mock the server with your own webserver and have it serve static http responses

  • Thanks for your answer! Can you please explain more about how you will test that on my place? Because as I understand integration test it is about testing your code in fully real environment, but as I mentioned service which I call through HTTP API is paid and I cannot call it for testing. – Eduard Romanyuk Dec 6 '20 at 7:43
  • create example responses in text files and host them yourself in a lite webserver that runs as part of the test – Ewan Dec 6 '20 at 10:36
0

End-to-end tests are a better way to ensure this code works. Many paid services offer a "test" environment that is not fully functional, but if also free to call from your application.

The logic implemented in this class, while not trivial, is not complex either. What would you unit test? That the response gets logged? Because this class relies so heavily on real outside services, it is better to test this in a real application environment.

From a unit testing perspective, the crucial thing you got right was having the TsMessageServiceImpl class implement an interface, which allows you to write unit tests for the classes that use TsMessageServiceImpl.

  • Yeah, I think so too, but service is paid and I cannot use real requests for testing. – Eduard Romanyuk Dec 6 '20 at 7:45
  • @EduardRomanyuk: I think you may have misunderstood my answer. The only real testing you can do for this kind of code is a real end-to-end test. It could very well cost money. See if those service providers have a "test" environment that you can use, which might be more economical. – Greg Burghardt Dec 6 '20 at 13:05
  • I updated my answer. – Greg Burghardt Dec 6 '20 at 13:08
  • I forgot to notice that they don't have test environment, unfortunately. I guess proposed above solution with mocking webserver is only way to go. Anyway thanks for paying attention to my question! – Eduard Romanyuk Dec 6 '20 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.