Skip to content

Commit

Permalink
chore: refactoring for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jan 18, 2025
1 parent 9fe269f commit eb9e4d8
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ public void beforeEach(final ExtensionContext extensionContext) throws Exception

private void resetWireMockServersIfConfigured(final ExtensionContext extensionContext) {
final List<Object> instances = extensionContext.getRequiredTestInstances().getAllInstances();
for (final Object instance : instances) {
final EnableWireMock enableWireMockAnnotation =
AnnotationUtils.findAnnotation(instance.getClass(), EnableWireMock.class);
if (enableWireMockAnnotation == null) {
continue;
}
for (EnableWireMock enableWireMockAnnotation : getEnableWireMockAnnotations(instances)) {
final ConfigureWireMock[] wireMockServers =
WireMockContextCustomizerFactory.getConfigureWireMocksOrDefault(
enableWireMockAnnotation.value());
Expand All @@ -61,12 +56,7 @@ private void configureWireMockForDefaultInstance(final ExtensionContext extensio
final List<Object> instances = extensionContext.getRequiredTestInstances().getAllInstances();
WireMockServer wiremock = null;
String wireMockName = null;
for (final Object instance : instances) {
final EnableWireMock enableWireMockAnnotation =
AnnotationUtils.findAnnotation(instance.getClass(), EnableWireMock.class);
if (enableWireMockAnnotation == null) {
continue;
}
for (EnableWireMock enableWireMockAnnotation : getEnableWireMockAnnotations(instances)) {
final ConfigureWireMock[] wireMockServers =
WireMockContextCustomizerFactory.getConfigureWireMocksOrDefault(
enableWireMockAnnotation.value());
Expand Down Expand Up @@ -100,6 +90,13 @@ private void configureWireMockForDefaultInstance(final ExtensionContext extensio
}
}

private List<EnableWireMock> getEnableWireMockAnnotations(List<Object> instances) {
return instances.stream()
.map(it -> AnnotationUtils.findAnnotation(it.getClass(), EnableWireMock.class))
.filter(it -> it != null)
.toList();
}

@Override
public void afterEach(final ExtensionContext context) throws Exception {
WireMock.configureFor(-1);
Expand All @@ -110,7 +107,6 @@ private static <T extends Annotation> void injectWireMockInstances(
final Class<T> annotation,
final Function<T, String> fn)
throws IllegalAccessException {
// getRequiredTestInstances() return multiple instances for nested tests
for (final Object testInstance :
extensionContext.getRequiredTestInstances().getAllInstances()) {
final List<Field> annotatedFields =
Expand Down

0 comments on commit eb9e4d8

Please sign in to comment.