diff --git a/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizer.java b/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizer.java index 523a167..ab35668 100644 --- a/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizer.java +++ b/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizer.java @@ -22,23 +22,28 @@ public class WireMockContextCustomizer implements ContextCustomizer { private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContextCustomizer.class); private final List configuration; + private final Class testClass; /** * Creates an instance of {@link WireMockContextCustomizer}. * * @param configurations the configurations */ - public WireMockContextCustomizer(final List configurations) { + public WireMockContextCustomizer( + final Class testClass, final List 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 @@ -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); } } diff --git a/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizerFactory.java b/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizerFactory.java index cc94065..dd97ee6 100644 --- a/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizerFactory.java +++ b/src/main/java/org/wiremock/spring/internal/WireMockContextCustomizerFactory.java @@ -33,7 +33,7 @@ public ContextCustomizer createContextCustomizer( if (holder.isEmpty()) { return null; } else { - return new WireMockContextCustomizer(holder.asArray()); + return new WireMockContextCustomizer(testClass, holder.asArray()); } }