Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only add default configured server if WireMock annotation applied to test class #54

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading