簡體   English   中英

如何在Spring Boot應用程序中的Feign客戶端上使用WireMock?

[英]How to use WireMock on a Feign client in a Spring Boot application?

我有一個使用Feign客戶端的課程。 之前我使用過Mockito並為Feign客戶端中的每個方法調用提供了存儲響應。 現在我想使用WireMock,以便我可以看到我的代碼正確處理不同類型的響應代碼。 我該怎么做呢? 我無法弄清楚如何在測試中連接我的Feign客戶端,並將其連接起來,以便它使用Wiremock而不是我在application.yml文件中設置的URL。 任何指針都將非常感激。

也許你想看看這個項目https://github.com/ePages-de/restdocs-wiremock

這有助於您在spring mvc測試中生成和發布wiremock片段(使用spring-rest-docs)。

最后,您可以使用這些代碼段啟動線程服務器,以便在測試中提供這些記錄的請求。

如果您回避這個集成解決方案,您可以在測試期間使用線纜模擬JUnit規則來啟動線程服務器。 http://wiremock.org/docs/junit-rule/

下面是一個示例測試,它使用動態線程模塊端口並配置功能區來使用此端口:(您使用的是假裝和功能區嗎?)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM