Skip to content

Commit

Permalink
Merge pull request #63 from wiremock/feature/issue-62-validate-name
Browse files Browse the repository at this point in the history
fix: give helpful error message when using same name in mocks (refs #62)
  • Loading branch information
tomasbjerre authored Nov 2, 2024
2 parents fdb382c + 790627d commit 9a1684b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
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;
Expand All @@ -23,11 +26,12 @@ public class WireMockContextCustomizerFactory implements ContextCustomizerFactor
@ConfigureWireMock(name = "wiremock")
private static class DefaultConfigureWireMock {}

static ConfigureWireMock[] getConfigureWireMocksOrDefault(final ConfigureWireMock... value) {
if (value == null || value.length == 0) {
static ConfigureWireMock[] getConfigureWireMocksOrDefault(
final ConfigureWireMock... configureWireMock) {
if (configureWireMock == null || configureWireMock.length == 0) {
return new ConfigureWireMock[] {WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK};
}
return value;
return configureWireMock;
}

@Override
Expand Down Expand Up @@ -56,6 +60,7 @@ private static class ConfigureWiremockHolder {

void add(final ConfigureWireMock... annotations) {
this.annotations.addAll(Arrays.asList(annotations));
this.sanityCheckDuplicateNames(this.annotations);
}

void parse(final Class<?> clazz) {
Expand All @@ -65,6 +70,19 @@ void parse(final Class<?> clazz) {
}
}

private void sanityCheckDuplicateNames(final List<ConfigureWireMock> check) {
final List<String> names = check.stream().map(it -> it.name()).toList();
final Set<String> dublicateNames =
names.stream()
.filter(it -> Collections.frequency(names, it) > 1)
.collect(Collectors.toSet());
if (!dublicateNames.isEmpty()) {
throw new IllegalStateException(
"Names of mocks must be unique, found duplicates of: "
+ dublicateNames.stream().sorted().collect(Collectors.joining(",")));
}
}

boolean isEmpty() {
return this.annotations.isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.wiremock.spring.test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;

import org.junit.jupiter.api.Test;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;
import org.wiremock.spring.internal.WireMockContextCustomizerFactory;

class ConfigurationValidationTest {

@EnableWireMock({@ConfigureWireMock, @ConfigureWireMock})
private static class EnableWireMockSameDefaultName {}

@EnableWireMock({@ConfigureWireMock(name = "w1"), @ConfigureWireMock(name = "w1")})
private static class EnableWireMockSameGivenName {}

@Test
void testDuplicateNames_default() {
final IllegalStateException thrown =
assertThrows(
IllegalStateException.class,
() ->
new WireMockContextCustomizerFactory()
.createContextCustomizer(EnableWireMockSameDefaultName.class, null));
assertThat(thrown.getMessage())
.isEqualTo("Names of mocks must be unique, found duplicates of: wiremock");
}

@Test
void testDuplicateNames_given() {
final IllegalStateException thrown =
assertThrows(
IllegalStateException.class,
() ->
new WireMockContextCustomizerFactory()
.createContextCustomizer(EnableWireMockSameGivenName.class, null));
assertThat(thrown.getMessage())
.isEqualTo("Names of mocks must be unique, found duplicates of: w1");
}
}

0 comments on commit 9a1684b

Please sign in to comment.