Skip to content

fix: avoid crash when only annotating super class (refs #78) #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading