Skip to content

Commit 6c1575c

Browse files
authored
Merge pull request #54 from wiremock/feature/server-fix
fix: only add default configured server if WireMock annotation applied to test class
2 parents 67d6784 + d5eaf0a commit 6c1575c

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/main/java/org/wiremock/spring/internal/WireMockContextCustomizerFactory.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* @author Maciej Walkowiak
1818
*/
1919
public class WireMockContextCustomizerFactory implements ContextCustomizerFactory {
20-
2120
private static final ConfigureWireMock DEFAULT_CONFIGURE_WIREMOCK =
2221
DefaultConfigureWireMock.class.getAnnotation(ConfigureWireMock.class);
2322

@@ -32,8 +31,7 @@ public ContextCustomizer createContextCustomizer(
3231
this.parseDefinitions(testClass, holder);
3332

3433
if (holder.isEmpty()) {
35-
return new WireMockContextCustomizer(
36-
WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK);
34+
return null;
3735
} else {
3836
return new WireMockContextCustomizer(holder.asArray());
3937
}
@@ -56,7 +54,12 @@ void add(final ConfigureWireMock... annotations) {
5654
void parse(final Class<?> clazz) {
5755
final EnableWireMock annotation = AnnotationUtils.findAnnotation(clazz, EnableWireMock.class);
5856
if (annotation != null) {
59-
this.add(annotation.value());
57+
final ConfigureWireMock[] value = annotation.value();
58+
if (value.length == 0) {
59+
this.add(WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK);
60+
} else {
61+
this.add(value);
62+
}
6063
}
6164
}
6265

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package app;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
import com.github.tomakehurst.wiremock.client.WireMock;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.core.env.Environment;
13+
import wiremock.org.apache.hc.client5.http.HttpHostConnectException;
14+
15+
@SpringBootTest
16+
class NotEnabledTest {
17+
18+
@Autowired private Environment env;
19+
20+
@Test
21+
void shouldNotHaveWireMockConfigured() {
22+
assertThrows(
23+
HttpHostConnectException.class,
24+
() -> WireMock.stubFor(get("/ping").willReturn(aResponse().withStatus(200))));
25+
26+
assertThat(this.env.getProperty("wiremock.server.baseUrl")).isNull();
27+
}
28+
}

0 commit comments

Comments
 (0)