Skip to content

Commit

Permalink
Merge pull request #80 from wiremock/fix/issue-78-parse-annotations-i…
Browse files Browse the repository at this point in the history
…n-super-class

fix: avoid crash when only annotating super class (refs #78)
  • Loading branch information
tomasbjerre authored Jan 19, 2025
2 parents eb9e4d8 + 942de68 commit 48594dc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.TestContextAnnotationUtils;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;

Expand Down Expand Up @@ -49,15 +49,28 @@ public ContextCustomizer createContextCustomizer(
}

private void parseDefinitions(final Class<?> testClass, final ConfigureWiremockHolder parser) {
parser.parse(testClass);
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
this.parseDefinitions(testClass.getEnclosingClass(), parser);
}
if (testClass.getSuperclass() != null) {
parseDefinitions(testClass.getSuperclass(), parser);
for (EnableWireMock enableWireMockAnnotation : getEnableWireMockAnnotations(testClass)) {
parser.add(getConfigureWireMocksOrDefault(enableWireMockAnnotation.value()));
}
}

private List<EnableWireMock> getEnableWireMockAnnotations(final Class<?> testClass) {
final List<EnableWireMock> annotations = new ArrayList<>();
Optional.ofNullable(AnnotationUtils.findAnnotation(testClass, EnableWireMock.class))
.ifPresent(it -> annotations.add(it));

Arrays.asList(testClass.getEnclosingClass(), testClass.getSuperclass()).stream()
.filter(clazz -> clazz != null)
.forEach(
clazz ->
annotations.addAll(
getEnableWireMockAnnotations(clazz).stream()
.filter(it -> !annotations.contains(it))
.toList()));

return annotations;
}

private static class ConfigureWiremockHolder {
private final List<ConfigureWireMock> annotations = new ArrayList<>();

Expand All @@ -69,13 +82,6 @@ void add(final ConfigureWireMock... annotations) {
this.sanityCheckUniquePorts(this.annotations);
}

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

private void sanityCheckDuplicateNames(final List<ConfigureWireMock> check) {
final List<String> names = check.stream().map(it -> it.name()).toList();
final Set<String> dublicateNames =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@ConfigureWireMock(name = "base-service-1", baseUrlProperties = "base-service-1.url"),
@ConfigureWireMock(name = "base-service-2", baseUrlProperties = "base-service-2.url")
})
class EnableWireMockOnSuperClassTest extends EnableWireMockOnSuperClassTestSuperClass {
class EnableWireMockOnSuperClassAndSubClassTest extends EnableWireMockOnSuperClassTestSuperClass {

@InjectWireMock("super-service-1")
private WireMockServer superService1;
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/usecases/EnableWireMockOnSuperClassOnlyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package usecases;

import static org.assertj.core.api.Assertions.assertThat;

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.InjectWireMock;

@SpringBootTest
class EnableWireMockOnSuperClassOnlyTest extends EnableWireMockOnSuperClassTestSuperClass {

@InjectWireMock("super-service-1")
private WireMockServer superService1;

@Value("${super-service-1.url}")
private String superService1Url;

@InjectWireMock("super-service-2")
private WireMockServer superService2;

@Value("${super-service-2.url}")
private String superService2Url;

@Test
void serversConfigured() {
assertThat(superService1).isNotNull();
assertThat(superService2).isNotNull();

assertThat(superService1Url).isNotEqualTo(superService2Url);
}
}

0 comments on commit 48594dc

Please sign in to comment.