W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
使用Spring Cloud Contract WireMock模塊,您可以在Spring Boot應(yīng)用程序中使用WireMock。查看 示例 以獲取更多詳細(xì)信息。
如果您有一個(gè)Spring Boot應(yīng)用程序,該應(yīng)用程序使用Tomcat作為嵌入式服務(wù)器(這是spring-boot-starter-web
的默認(rèn)設(shè)置),則可以將spring-cloud-starter-contract-stub-runner
添加到您的類(lèi)路徑中,并添加@AutoConfigureWireMock
,以便能夠在測(cè)試中使用Wiremock。Wiremock作為存根服務(wù)器運(yùn)行,您可以在測(cè)試中使用Java API或通過(guò)靜態(tài)JSON聲明來(lái)注冊(cè)存根行為。以下代碼顯示了一個(gè)示例:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @AutoConfigureWireMock(port = 0) public class WiremockForDocsTests { // A service that calls out over HTTP @Autowired private Service service; @Before public void setup() { this.service.setBase("http://localhost:" + this.environment.getProperty("wiremock.server.port")); } // Using the WireMock APIs in the normal way: @Test public void contextLoads() throws Exception { // Stubbing WireMock stubFor(get(urlEqualTo("/resource")).willReturn(aResponse() .withHeader("Content-Type", "text/plain").withBody("Hello World!"))); // We're asserting if WireMock responded properly assertThat(this.service.go()).isEqualTo("Hello World!"); } }
要在其他端口上啟動(dòng)存根服務(wù)器,請(qǐng)使用@AutoConfigureWireMock(port=9999)
。對(duì)于隨機(jī)端口,請(qǐng)使用值0
??梢栽跍y(cè)試應(yīng)用程序上下文中使用“ wiremock.server.port”屬性綁定存根服務(wù)器端口。使用@AutoConfigureWireMock
將類(lèi)型為WiremockConfiguration
的bean添加到測(cè)試應(yīng)用程序上下文中,該變量將被緩存在具有相同上下文的方法和類(lèi)之間,與Spring集成測(cè)試相同。您也可以將WireMockServer
類(lèi)型的bean注入測(cè)試中。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: