Spock存根RestTemplate方法

时间:2019-05-13 15:04:37

标签: spring-boot testing resttemplate spock stub

我正在尝试为客户端类编写测试,该客户端类使用RestTemplate从云中获取文件(getSingleFileContentMap()方法)。

这是客户端类的完整代码:

package i18n.translationbundle.client;

import i18n.configuration.TranslationProperties;
import i18n.translationbundle.TranslationBundle;
import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

    @Component
    @RequiredArgsConstructor
        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();
            }

        }

我的目标是在测试中存根getSingleFileContent(),因此我不必依赖连接。这是我尝试将其存根的两种方法:

client.getSingleFileContentMap("dashboard") >> mapper.readValue(
            dashboardJson,
            new TypeReference<Map<String, String>>(){})

template.exchange("src/test/resources/en-us/dashboard.lang.json", HttpMethod.GET, null, new ParameterizedTypeReference<Map<String, String>>(){}) >>
            mapper.readValue(
                    dashboardJson,
                    new TypeReference<Map<String, String>>(){})

但是,我不断收到以下错误消息:

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>

我在做什么错?存根控件是否应该在不干预方法实现的情况下盲目返回我想要的内容?在这里,它似乎仍在以客户端类代码中的方式运行getSingleFileContentMap()。我了解对Rest Template的{​​{1}}方法进行存根操作可能无效,但是为什么它不适用于exchange()

0 个答案:

没有答案