Skip to content

@EnableWireMock on parent class causes "Names of mocks must be unique, found duplicates of: wiremock" #78

Closed
@kzander91

Description

@kzander91

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:

if (testClass.getSuperclass() != null) {
parseDefinitions(testClass.getSuperclass(), parser);
}

For each class in the hierarchy, Spring's AnnotationUtils.findAnnotation() is used:

void parse(final Class<?> clazz) {
final EnableWireMock annotation = AnnotationUtils.findAnnotation(clazz, EnableWireMock.class);
if (annotation != null) {
this.add(getConfigureWireMocksOrDefault(annotation.value()));
}

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:
Image

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() {
    }

}

References

#75
#76

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions