Skip to content

Commit

Permalink
fix: did not always customize context
Browse files Browse the repository at this point in the history
CustomizeContext did not always run because of caching issue, documented in the code.

Resulting in unstable test cases.
  • Loading branch information
tomasbjerre committed Oct 11, 2024
1 parent 6c1575c commit 879a8e1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,28 @@ public class WireMockContextCustomizer implements ContextCustomizer {
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContextCustomizer.class);

private final List<ConfigureWireMock> configuration;
private final Class<?> testClass;

/**
* Creates an instance of {@link WireMockContextCustomizer}.
*
* @param configurations the configurations
*/
public WireMockContextCustomizer(final List<ConfigureWireMock> configurations) {
public WireMockContextCustomizer(
final Class<?> testClass, final List<ConfigureWireMock> configurations) {
this.configuration = configurations;
this.testClass = testClass;
}

/**
* Creates an instance of {@link WireMockContextCustomizer}.
*
* @param testClass
* @param configurations the configurations
*/
public WireMockContextCustomizer(final ConfigureWireMock... configurations) {
this(Arrays.asList(configurations));
public WireMockContextCustomizer(
final Class<?> testClass, final ConfigureWireMock... configurations) {
this(testClass, Arrays.asList(configurations));
}

@Override
Expand Down Expand Up @@ -67,20 +72,32 @@ private WireMockServer resolveOrCreateWireMockServer(
return wireMockServer;
}

/**
* The docs in {@link ContextCustomizer} states that equals and hashcode is being used for caching
* and needs implementation. If test class is not included it will not be unique and
* customizeContext will not be invoked for all tests.
*/
@Override
public boolean equals(final Object o) {
if (this == o) {
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final WireMockContextCustomizer that = (WireMockContextCustomizer) o;
return Objects.equals(this.configuration, that.configuration);
final WireMockContextCustomizer other = (WireMockContextCustomizer) obj;
return Objects.equals(this.configuration, other.configuration)
&& Objects.equals(this.testClass, other.testClass);
}

/**
* @see #equals
*/
@Override
public int hashCode() {
return Objects.hash(this.configuration);
return Objects.hash(this.configuration, this.testClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ContextCustomizer createContextCustomizer(
if (holder.isEmpty()) {
return null;
} else {
return new WireMockContextCustomizer(holder.asArray());
return new WireMockContextCustomizer(testClass, holder.asArray());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.wiremock.spring.internal;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Function;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
Expand All @@ -18,7 +20,8 @@
*
* @author Maciej Walkowiak
*/
public class WireMockSpringExtension implements BeforeEachCallback, ParameterResolver {
public class WireMockSpringExtension
implements BeforeEachCallback, AfterEachCallback, ParameterResolver {

@Override
public void beforeEach(final ExtensionContext extensionContext) throws Exception {
Expand All @@ -29,6 +32,11 @@ public void beforeEach(final ExtensionContext extensionContext) throws Exception
injectWireMockInstances(extensionContext, InjectWireMock.class, InjectWireMock::value);
}

@Override
public void afterEach(final ExtensionContext context) throws Exception {
WireMock.configureFor(8080);
}

private static <T extends Annotation> void injectWireMockInstances(
final ExtensionContext extensionContext,
final Class<T> annotation,
Expand Down

0 comments on commit 879a8e1

Please sign in to comment.