StubRunnerExtension can't be registered around non static field

fixes gh-1458
This commit is contained in:
Marcin Grzejszczak
2020-07-31 11:27:15 +02:00
parent 90be2f0b50
commit fa07ca140c
3 changed files with 116 additions and 4 deletions

View File

@@ -26,7 +26,9 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.cloud.contract.spec.Contract;
@@ -48,7 +50,7 @@ import org.springframework.cloud.contract.verifier.messaging.MessageVerifier;
* @author Olga Maciaszek-Sharma
* @since 2.1.0
*/
public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback,
StubFinder, StubRunnerExtensionOptions {
private static final String DELIMITER = ":";
@@ -73,15 +75,33 @@ public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
this.delegate = delegate;
}
@Override
public void afterAll(ExtensionContext extensionContext) {
after();
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
after();
}
@Override
public void beforeAll(ExtensionContext extensionContext) {
before();
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
before();
}
private void before() {
stubFinder(new BatchStubRunnerFactory(builder().build(), verifier())
.buildBatchStubRunner());
stubFinder().runStubs();
}
@Override
public void afterAll(ExtensionContext extensionContext) {
private void after() {
try {
stubFinder().close();
}
@@ -90,6 +110,7 @@ public class StubRunnerExtension implements BeforeAllCallback, AfterAllCallback,
}
}
@Override
public URL findStubUrl(String groupId, String artifactId)
throws StubNotFoundException {

View File

@@ -199,7 +199,7 @@ public class StubRunnerRule implements TestRule, StubFinder, StubRunnerRuleOptio
@Override
public StubRunnerRule failOnNoStubs(boolean failOnNoStubs) {
builder().withFailOnNoStubs(failOnNoStubs);
return null;
return this.delegate;
}
@Override

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.contract.stubrunner.junit;
import java.io.File;
import java.net.URL;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Olga Maciaszek-Sharma
* @since 2.1.0
*/
class StubRunnerJUnit5MethodExtensionTests {
// tag::extension[]
// Visible for Junit
@RegisterExtension
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");
@BeforeEach
@AfterEach
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 "";
}
}
// end::extension[]
@Test
void should_start_WireMock_servers() {
assertThat(stubRunnerExtension.findStubUrl(
"org.springframework.cloud.contract.verifier.stubs", "loanIssuance"))
.isNotNull();
assertThat(stubRunnerExtension.findStubUrl("loanIssuance")).isNotNull();
assertThat(stubRunnerExtension.findStubUrl("loanIssuance"))
.isEqualTo(stubRunnerExtension.findStubUrl(
"org.springframework.cloud.contract.verifier.stubs",
"loanIssuance"));
assertThat(stubRunnerExtension.findStubUrl(
"org.springframework.cloud.contract.verifier.stubs:fraudDetectionServer"))
.isNotNull();
}
@Test
void should_output_mappings_to_output_folder() {
// when
URL url = stubRunnerExtension.findStubUrl("fraudDetectionServer");
// then
assertThat(new File("target/outputmappingsforrule",
"fraudDetectionServer_" + url.getPort())).exists();
}
}