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 99c2fde commit 16813c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 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

0 comments on commit 16813c0

Please sign in to comment.