2016-09-19 142 views
7

我有一个使用Feign客户端的类。之前我使用Mockito并为Feign客户端中的每个方法调用给出了一个存储响应。现在我想使用WireMock,以便我可以看到我的代码正确处理了不同类型的响应代码。我如何去做这件事?我无法弄清楚如何在测试中连接我的Feign客户端,并将它连接起来,以便它使用Wiremock而不是我在我的application.yml文件中设置的URL。任何指针将不胜感激。如何在Spring Boot应用程序的Feign客户端上使用WireMock?

回答

3

也许你想在这个项目https://github.com/ePages-de/restdocs-wiremock

这可以帮助您生成并在Spring MVC的测试发布wiremock片段看(用弹簧REST的文档)。

最后,您可以使用这些片段来启动一个线连接服务器,以便在测试中提供这些记录的请求。

如果您回避这个集成解决方案,您可以使用Wiremock JUnit规则在测试期间触发您的电线连接服务器。 http://wiremock.org/docs/junit-rule/

下面是一个使用动态wiremock端口,并配置色带使用该端口进行了抽样检测:(您使用的假死和色带?)

@WebAppConfiguration 
    @RunWith(SpringRunner.class) 
    @SpringBootTest() 
    @ActiveProfiles({"test","wiremock"}) 
    public class ServiceClientIntegrationTest { 

     @Autowired //this is the FeignClient service interface 
     public ServiceClient serviceClient; 

     @ClassRule 
     public static WireMockRule WIREMOCK = new WireMockRule(
       wireMockConfig().fileSource(new ClasspathFileSource("path/to/wiremock/snipptes")).dynamicPort()); 

     @Test 
     public void createSome() { 
      ServiceClient.Some t = serviceClient.someOperation(new Some("some")); 
      assertTrue(t.getId() > 0); 
     } 

//using dynamic ports requires to configure the ribbon server list accordingly 
     @Profile("wiremock") 
     @Configuration 
     public static class TestConfiguration { 

      @Bean 
      public ServerList<Server> ribbonServerList() { 
       return new StaticServerList<>(new Server("localhost", WIREMOCK.port())); 
      } 
     } 
    } 
+0

感谢您的回答!我没有使用丝带 - 只有假装。我使用'@ FeignClient'和'url = externalApiUrl'。我怎样才能在那里注入wiremock url呢? – L42

+0

wiremock实际上是运行一个服务器,所以只要确保FeignClient指向'localhost:WIREMOCK.port'即可。我不确定url是否可以指向配置属性。它似乎并不支持春天的表达。在类似的问题,有一个动态feign网址接受的答案建议使用功能区和上面的测试使用的配置机制。 http://stackoverflow.com/a/29278126/5371736 –

+0

在经历了很多与春季版本的斗争之后,我认为我更接近了。但是,我的自动装配假客户端被设置为“空”。你知道这是为什么吗?我目前的计划是尝试在@SpringBootTest中将连线URL设置为属性,以便假装客户端(如果不为null)将进入连线模式。 – L42