W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Stub Runner帶有JUnit規(guī)則,因此您可以很容易地下載和運行給定組和工件ID的存根:
@ClassRule public static StubRunnerRule rule = new StubRunnerRule().repoRoot(repoRoot()) .stubsMode(StubRunnerProperties.StubsMode.REMOTE) .downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance") .downloadStub( "org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer"); @BeforeClass @AfterClass public static void setupProps() { System.clearProperty("stubrunner.repository.root"); System.clearProperty("stubrunner.classifier"); }
JUnit 5也有一個StubRunnerExtension
。StubRunnerRule
和StubRunnerExtension
的工作方式非常相似。執(zhí)行完規(guī)則/擴展名后,Stub Runner將連接到Maven存儲庫,并針對給定的依賴項列表嘗試執(zhí)行以下操作:
MessageVerifier
接口的實現(xiàn))Stub Runner使用Eclipse Aether機制下載Maven依賴項。查看他們的文檔以獲取更多信息。
由于StubRunnerRule
和StubRunnerExtension
實現(xiàn)了StubFinder
,因此它們使您可以找到已啟動的存根:
package org.springframework.cloud.contract.stubrunner; import java.net.URL; import java.util.Collection; import java.util.Map; import org.springframework.cloud.contract.spec.Contract; /** * Contract for finding registered stubs. * * @author Marcin Grzejszczak */ public interface StubFinder extends StubTrigger { /** * For the given groupId and artifactId tries to find the matching URL of the running * stub. * @param groupId - might be null. In that case a search only via artifactId takes * place * @param artifactId - artifact id of the stub * @return URL of a running stub or throws exception if not found * @throws StubNotFoundException in case of not finding a stub */ URL findStubUrl(String groupId, String artifactId) throws StubNotFoundException; /** * For the given Ivy notation {@code [groupId]:artifactId:[version]:[classifier]} * tries to find the matching URL of the running stub. You can also pass only * {@code artifactId}. * @param ivyNotation - Ivy representation of the Maven artifact * @return URL of a running stub or throws exception if not found * @throws StubNotFoundException in case of not finding a stub */ URL findStubUrl(String ivyNotation) throws StubNotFoundException; /** * @return all running stubs */ RunningStubs findAllRunningStubs(); /** * @return the list of Contracts */ Map<StubConfiguration, Collection<Contract>> getContracts(); }
Spock測試中的用法示例:
@ClassRule @Shared StubRunnerRule rule = new StubRunnerRule() .stubsMode(StubRunnerProperties.StubsMode.REMOTE) .repoRoot(StubRunnerRuleSpec.getResource("/m2repo/repository").toURI().toString()) .downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance") .downloadStub("org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer") .withMappingsOutputFolder("target/outputmappingsforrule") def 'should start WireMock servers'() { expect: 'WireMocks are running' rule.findStubUrl('org.springframework.cloud.contract.verifier.stubs', 'loanIssuance') != null rule.findStubUrl('loanIssuance') != null rule.findStubUrl('loanIssuance') == rule.findStubUrl('org.springframework.cloud.contract.verifier.stubs', 'loanIssuance') rule.findStubUrl('org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer') != null and: rule.findAllRunningStubs().isPresent('loanIssuance') rule.findAllRunningStubs().isPresent('org.springframework.cloud.contract.verifier.stubs', 'fraudDetectionServer') rule.findAllRunningStubs().isPresent('org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer') and: 'Stubs were registered' "${rule.findStubUrl('loanIssuance').toString()}/name".toURL().text == 'loanIssuance' "${rule.findStubUrl('fraudDetectionServer').toString()}/name".toURL().text == 'fraudDetectionServer' } def 'should output mappings to output folder'() { when: def url = rule.findStubUrl('fraudDetectionServer') then: new File("target/outputmappingsforrule", "fraudDetectionServer_${url.port}").exists() }
JUnit測試中的用法示例:
@Test public void should_start_wiremock_servers() throws Exception { // expect: 'WireMocks are running' then(rule.findStubUrl("org.springframework.cloud.contract.verifier.stubs", "loanIssuance")).isNotNull(); then(rule.findStubUrl("loanIssuance")).isNotNull(); then(rule.findStubUrl("loanIssuance")).isEqualTo(rule.findStubUrl( "org.springframework.cloud.contract.verifier.stubs", "loanIssuance")); then(rule.findStubUrl( "org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer")) .isNotNull(); // and: then(rule.findAllRunningStubs().isPresent("loanIssuance")).isTrue(); then(rule.findAllRunningStubs().isPresent( "org.springframework.cloud.contract.verifier.stubs", "fraudDetectionServer")).isTrue(); then(rule.findAllRunningStubs().isPresent( "org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer")) .isTrue(); // and: 'Stubs were registered' then(httpGet(rule.findStubUrl("loanIssuance").toString() + "/name")) .isEqualTo("loanIssuance"); then(httpGet(rule.findStubUrl("fraudDetectionServer").toString() + "/name")) .isEqualTo("fraudDetectionServer"); } private String httpGet(String url) throws Exception { try (InputStream stream = URI.create(url).toURL().openStream()) { return StreamUtils.copyToString(stream, Charset.forName("UTF-8")); } } }
JUnit 5擴展示例:
// Visible for Junit @RegisterExtension static StubRunnerExtension stubRunnerExtension = new StubRunnerExtension() .repoRoot(repoRoot()).stubsMode(StubRunnerProperties.StubsMode.REMOTE) .downloadStub("org.springframework.cloud.contract.verifier.stubs", "loanIssuance") .downloadStub( "org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer") .withMappingsOutputFolder("target/outputmappingsforrule"); @BeforeAll @AfterAll static void setupProps() { System.clearProperty("stubrunner.repository.root"); System.clearProperty("stubrunner.classifier"); } private static String repoRoot() { try { return StubRunnerRuleJUnitTest.class.getResource("/m2repo/repository/") .toURI().toString(); } catch (Exception e) { return ""; } }
檢查JUnit和Spring的Common屬性,以獲取有關(guān)如何應用Stub Runner全局配置的更多信息。
要將JUnit規(guī)則或JUnit 5擴展與消息傳遞一起使用,您必須向規(guī)則構(gòu)建器(例如
rule.messageVerifier(new MyMessageVerifier())
)提供MessageVerifier
接口的實現(xiàn)。如果不這樣做,則每當您嘗試發(fā)送消息時,都會引發(fā)異常。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: