2016-09-19 36 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

相關問題