Skip to content

Commit

Permalink
Merge pull request #54 from wiremock/feature/server-fix
Browse files Browse the repository at this point in the history
fix: only add default configured server if WireMock annotation applied to test class
  • Loading branch information
tomasbjerre authored Oct 11, 2024
2 parents 67d6784 + d5eaf0a commit 6c1575c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* @author Maciej Walkowiak
*/
public class WireMockContextCustomizerFactory implements ContextCustomizerFactory {

private static final ConfigureWireMock DEFAULT_CONFIGURE_WIREMOCK =
DefaultConfigureWireMock.class.getAnnotation(ConfigureWireMock.class);

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

if (holder.isEmpty()) {
return new WireMockContextCustomizer(
WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK);
return null;
} else {
return new WireMockContextCustomizer(holder.asArray());
}
Expand All @@ -56,7 +54,12 @@ void add(final ConfigureWireMock... annotations) {
void parse(final Class<?> clazz) {
final EnableWireMock annotation = AnnotationUtils.findAnnotation(clazz, EnableWireMock.class);
if (annotation != null) {
this.add(annotation.value());
final ConfigureWireMock[] value = annotation.value();
if (value.length == 0) {
this.add(WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK);
} else {
this.add(value);
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions wiremock-spring-boot-example/src/test/java/app/NotEnabledTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package app;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import wiremock.org.apache.hc.client5.http.HttpHostConnectException;

@SpringBootTest
class NotEnabledTest {

@Autowired private Environment env;

@Test
void shouldNotHaveWireMockConfigured() {
assertThrows(
HttpHostConnectException.class,
() -> WireMock.stubFor(get("/ping").willReturn(aResponse().withStatus(200))));

assertThat(this.env.getProperty("wiremock.server.baseUrl")).isNull();
}
}

0 comments on commit 6c1575c

Please sign in to comment.