Fixing issues with duplicated init
This commit is contained in:
@@ -56,6 +56,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Matt Garner
|
||||
* @author Waldemar Panas
|
||||
*
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@@ -114,6 +115,12 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
updateCurrentServer();
|
||||
}
|
||||
|
||||
void initIfNotRunning() throws IOException {
|
||||
if (!this.running) {
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
private void reRegisterBeans() {
|
||||
if (!this.beanFactory.containsBean(WIREMOCK_SERVER_BEAN_NAME)) {
|
||||
this.beanFactory.registerSingleton(WIREMOCK_SERVER_BEAN_NAME, this.server);
|
||||
@@ -170,7 +177,7 @@ public class WireMockConfiguration implements SmartLifecycle {
|
||||
private void registerStubs() {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Will register [" + this.wireMock.getServer().getStubs().length
|
||||
+ "] stubs");
|
||||
+ "] stub locations");
|
||||
}
|
||||
for (String stubs : this.wireMock.getServer().getStubs()) {
|
||||
if (StringUtils.hasText(stubs)) {
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
* @author Matt Garner
|
||||
* @author Waldemar Panas
|
||||
* @since 1.2.6
|
||||
*/
|
||||
public final class WireMockTestExecutionListener extends AbstractTestExecutionListener {
|
||||
@@ -46,7 +47,7 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Re-registering default mappings");
|
||||
}
|
||||
wireMockConfig(testContext).init();
|
||||
wireMockConfig(testContext).initIfNotRunning();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.wiremock.issues.staticInit;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "my-client")
|
||||
public class ClientProperties {
|
||||
|
||||
private String url;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.wiremock.issues.staticInit;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(ClientProperties.class)
|
||||
public class RestTemplateConfiguration {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.wiremock.issues.staticInit;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TimeoutApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TimeoutApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.wiremock.issues.staticInit;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Component
|
||||
public class WebClient {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
private final ClientProperties clientProperties;
|
||||
|
||||
public WebClient(RestTemplate restTemplate, ClientProperties clientProperties) {
|
||||
this.restTemplate = restTemplate;
|
||||
this.clientProperties = clientProperties;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
URI url = URI.create(clientProperties.getUrl());
|
||||
return restTemplate.getForObject(url, String.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2013-2019 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.wiremock.issues.staticInit;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import org.assertj.core.api.BDDAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
|
||||
@SpringBootTest(
|
||||
properties = "my-client.url=http://localhost:${wiremock.server.port}/resources")
|
||||
@AutoConfigureWireMock(port = 0, httpsPort = 0, files = "src/test/resources/__files")
|
||||
class WebClientTest {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(WebClientTest.class);
|
||||
|
||||
@Autowired
|
||||
WebClient underTest;
|
||||
|
||||
@Autowired
|
||||
Stubber stubber;
|
||||
|
||||
@Test
|
||||
void shouldRespondForGetJustFine() {
|
||||
String actual = underTest.get();
|
||||
|
||||
BDDAssertions.then(actual).isEqualTo("Everything seems fine!");
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class WebClientTestConfiguration {
|
||||
|
||||
@Bean
|
||||
Stubber stubberWithoutDeps() {
|
||||
return new Stubber();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class Stubber {
|
||||
|
||||
Stubber() {
|
||||
log.info("Setting up GET");
|
||||
stubFor(get(urlEqualTo("/resources"))
|
||||
.willReturn(aResponse().withBody("Everything seems fine!")));
|
||||
}
|
||||
|
||||
Stubber(WireMockServer wireMockServer) {
|
||||
log.info("Setting up GET");
|
||||
wireMockServer.stubFor(get(urlEqualTo("/resources"))
|
||||
.willReturn(aResponse().withBody("Everything seems fine!")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user