Closed
Description
Proposal
Since #76 in 3.5.0, when @EnableWireMock
is present on a parent class, test initalization fails with
java.lang.IllegalStateException: Names of mocks must be unique, found duplicates of: wiremock
at org.wiremock.spring.internal.WireMockContextCustomizerFactory$ConfigureWiremockHolder.sanityCheckDuplicateNames(WireMockContextCustomizerFactory.java:88)
at org.wiremock.spring.internal.WireMockContextCustomizerFactory$ConfigureWiremockHolder.add(WireMockContextCustomizerFactory.java:66)
at org.wiremock.spring.internal.WireMockContextCustomizerFactory$ConfigureWiremockHolder.parse(WireMockContextCustomizerFactory.java:75)
The bug is caused by the parsing algorithm, which checks each class in the test class hierarchy:
For each class in the hierarchy, Spring's AnnotationUtils.findAnnotation()
is used:
However, AnnotationUtils.findAnnotation()
already checks parent classes (see Javadoc), so when a class is processed that inherits from a class that declares @EnableWireMock
, the annotation is already found there. Then, when the parent class itself is processed, the same annotation is found again, causing the issue.
Here you can see that the same annotation instance is present twice, causing the duplicate check to fail:
Reproduction steps
Parent class:
package com.example.demo;
import org.wiremock.spring.EnableWireMock;
@EnableWireMock
class ParentTestClass {
}
Child class:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Demo2ApplicationTests extends ParentTestClass {
@Test
void contextLoads() {
}
}