Hoverfly 为您运行一个模拟服务器 - 它不知道这样的存在RestTemplate。您有责任告诉其余模板请求正确的 Hoverfly 端点。
application.properties一种可能的解决方案是在您的实例中将端点作为配置条目:
external-resource.endpoint=http://fancy-stuff.com
并将其注入您的班级:
@Value("${external-resource.endpoint}")
private String endpoint;
restTemplate.get(endpoint, ...);
然后,您可以为您的测试专门配置 application-test.properties 配置,您可以在其中拥有指向 Hoverfly 模拟端点的正确端点:
#Hoverfly mocked endpoint
external-resource.endpoint=http://localhost:9000
在这种情况下,您唯一需要做的就是放入@ActiveProfiles("test")您的测试类,这将激活“测试”配置文件并将端点值指向 Hoverfly。
希望这是有道理的!