W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
您可以選擇以下獲取存根的選項
org.springframework.cloud.contract.stubrunner.StubDownloaderBuilder
實現(xiàn)以進行完全自定義后一個示例在“ 自定義Stub Runner”部分中進行了描述。
您可以通過stubsMode
開關(guān)控制存根下載。它從StubRunnerProperties.StubsMode
枚舉中選擇值。您可以使用以下選項
StubRunnerProperties.StubsMode.CLASSPATH
(默認值)-將從類路徑中選擇存根StubRunnerProperties.StubsMode.LOCAL
-將從本地存儲區(qū)中選擇存根(例如.m2
)StubRunnerProperties.StubsMode.REMOTE
-將從遠程位置選擇存根例:
@AutoConfigureStubRunner(repositoryRoot="https://foo.bar", ids = "com.example:beer-api-producer:+:stubs:8095", stubsMode = StubRunnerProperties.StubsMode.LOCAL)
如果將stubsMode
屬性設(shè)置為StubRunnerProperties.StubsMode.CLASSPATH
(或由于默認值CLASSPATH
而未設(shè)置任何內(nèi)容),則將掃描類路徑。讓我們看下面的例子:
@AutoConfigureStubRunner(ids = { "com.example:beer-api-producer:+:stubs:8095", "com.example.foo:bar:1.0.0:superstubs:8096" })
如果您已將依賴項添加到類路徑中
Maven.
<dependency> <groupId>com.example</groupId> <artifactId>beer-api-producer-restdocs</artifactId> <classifier>stubs</classifier> <version>0.0.1-SNAPSHOT</version> <scope>test</scope> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.example.foo</groupId> <artifactId>bar</artifactId> <classifier>superstubs</classifier> <version>1.0.0</version> <scope>test</scope> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency>
Gradle.
testCompile("com.example:beer-api-producer-restdocs:0.0.1-SNAPSHOT:stubs") { transitive = false } testCompile("com.example.foo:bar:1.0.0:superstubs") { transitive = false }
然后,將掃描您的類路徑上的以下位置。對于com.example:beer-api-producer-restdocs
和com.example.foo:bar
如您所見,打包生產(chǎn)者存根時必須顯式提供組和工件ID。
生產(chǎn)者將像這樣設(shè)置合同:
└── src
└── test
└── resources
└── contracts
└── com.example
└── beer-api-producer-restdocs
└── nested
└── contract3.groovy
要實現(xiàn)正確的存根包裝。
或使用Maven assembly
插件或
Gradle Jar任務(wù),您必須在存根jar中創(chuàng)建以下結(jié)構(gòu)。
└── META-INF └── com.example └── beer-api-producer-restdocs └── 2.0.0 ├── contracts │ └── nested │ └── contract2.groovy └── mappings └── mapping.json
通過維護這種結(jié)構(gòu),可以掃描類路徑,而無需下載工件即可從消息傳遞/ HTTP存根中受益。
Stub Runner具有HttpServerStub
的概念,該概念抽象了HTTP服務(wù)器的底層具體實現(xiàn)(例如,WireMock是實現(xiàn)之一)。有時,您需要對存根服務(wù)器執(zhí)行一些其他調(diào)整,這對于給定的實現(xiàn)而言是具體的。為此,Stub Runner為您提供了httpServerStubConfigurer
屬性,該屬性在批注JUnit規(guī)則中可用,并且可以通過系統(tǒng)屬性進行訪問,您可以在其中提供org.springframework.cloud.contract.stubrunner.HttpServerStubConfigurer
接口的實現(xiàn)。這些實現(xiàn)可以更改給定HTTP服務(wù)器存根的配置文件。
Spring Cloud Contract Stub Runner帶有一個可以擴展的實現(xiàn),適用于WireMock-org.springframework.cloud.contract.stubrunner.provider.wiremock.WireMockHttpServerStubConfigurer
。在configure
方法中,您可以為給定的存根提供自己的自定義配置。用例可能是在HTTPs端口上為給定的工件ID啟動WireMock。例:
WireMockHttpServerStubConfigurer實現(xiàn)。
@CompileStatic static class HttpsForFraudDetection extends WireMockHttpServerStubConfigurer { private static final Log log = LogFactory.getLog(HttpsForFraudDetection) @Override WireMockConfiguration configure(WireMockConfiguration httpStubConfiguration, HttpServerStubConfiguration httpServerStubConfiguration) { if (httpServerStubConfiguration.stubConfiguration.artifactId == "fraudDetectionServer") { int httpsPort = SocketUtils.findAvailableTcpPort() log.info("Will set HTTPs port [" + httpsPort + "] for fraud detection server") return httpStubConfiguration .httpsPort(httpsPort) } return httpStubConfiguration } }
然后,您可以通過注釋重用它
@AutoConfigureStubRunner(mappingsOutputFolder = "target/outputmappings/",
httpServerStubConfigurer = HttpsForFraudDetection)
只要找到一個https端口,它將優(yōu)先于http端口。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: