lundi 13 mai 2019

Spock stubbing RestTemplate method

I'm trying to write tests for my client class, which uses RestTemplate to get files from my cloud (getSingleFileContentMap() method).

This is the full code of the client class:

public class DotCMSClient implements TranslationBundleSupplier {

    private final TranslationProperties translationProperties;
    private final RestTemplate template;

    @Override
    public TranslationBundle getTranslationBundle(String language) {
        Set<Map<String, String>> set = getTranslationsSetForLanguage(language);
        return new TranslationBundle(
                set.stream()
                        .flatMap(x -> x.entrySet().stream())
                        .collect(Collectors.toMap(
                                Map.Entry::getKey,
                                Map.Entry::getValue,
                                (v1, v2) -> v2
                        )));
    }

    private Set<Map<String, String>> getTranslationsSetForLanguage(String language) {
        return translationProperties.getResourceUrls()
                .get(language)
                .values()
                .stream()
                .map(this::getSingleFileContentMap)
                .collect(Collectors.toSet());
    }

    private Map<String, String> getSingleFileContentMap(String location) {
        String domainAddress = translationProperties.getSourceDomain();
        return template.exchange(domainAddress + location, HttpMethod.GET, null, new ParameterizedTypeReference<Map<String, String>>(){}).getBody();
    }

}

My goal is to stub the getSingleFileContent() in my tests, so I won't have to rely on the connection. These are two ways how I've tried to stub it:

client.getSingleFileContentMap("dashboard") >> new String(Files.readAllBytes(Paths.get("src/test/resources/en-us/dashboard.lang.json")))

and

template.exchange("src/test/resources/en-us/dashboard.lang.json", HttpMethod.GET, null, new ParameterizedTypeReference<Map<String, String>>(){}) >>
                Files.readAllBytes(Paths.get("src/test/resources/en-us/dashboard.lang.json"))

However, I keep getting the following error:

client.getSingleFileContentMap("dashboard")
|      |
|      java.lang.IllegalArgumentException: URI is not absolute
<translationbundle.client.DotCMSClient@b978d10 translationProperties=TranslationProperties(sourceDomain=, fileNames=null, languages=[en-us], resourceUrls={en-us={dashboard=src/test/resources/en-us/dashboard.lang.json, ip-block=src/test/resources/en-us/ip-block.lang.json, general=src/test/resources/en-us/general.lang.json}}) template=org.springframework.web.client.RestTemplate@2ce6c6ec>

What am I doing wrong? Isn't stubbing supposed to blindly return what I want, without intervening into the methods implementation? Here, it seems that it's still running getSingleFileContentMap() the way it was implemented in the client class code. I understand that stubbing Rest Template's exchange() method might not work, but why doesn't it work with getSingleFileContentMap()?

Aucun commentaire:

Enregistrer un commentaire