Skip to content

Commit 9a1684b

Browse files
authored
Merge pull request #63 from wiremock/feature/issue-62-validate-name
fix: give helpful error message when using same name in mocks (refs #62)
2 parents fdb382c + 790627d commit 9a1684b

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/main/java/org/wiremock/spring/internal/WireMockContextCustomizerFactory.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Collections;
56
import java.util.List;
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
69
import org.springframework.core.annotation.AnnotationUtils;
710
import org.springframework.test.context.ContextConfigurationAttributes;
811
import org.springframework.test.context.ContextCustomizer;
@@ -23,11 +26,12 @@ public class WireMockContextCustomizerFactory implements ContextCustomizerFactor
2326
@ConfigureWireMock(name = "wiremock")
2427
private static class DefaultConfigureWireMock {}
2528

26-
static ConfigureWireMock[] getConfigureWireMocksOrDefault(final ConfigureWireMock... value) {
27-
if (value == null || value.length == 0) {
29+
static ConfigureWireMock[] getConfigureWireMocksOrDefault(
30+
final ConfigureWireMock... configureWireMock) {
31+
if (configureWireMock == null || configureWireMock.length == 0) {
2832
return new ConfigureWireMock[] {WireMockContextCustomizerFactory.DEFAULT_CONFIGURE_WIREMOCK};
2933
}
30-
return value;
34+
return configureWireMock;
3135
}
3236

3337
@Override
@@ -56,6 +60,7 @@ private static class ConfigureWiremockHolder {
5660

5761
void add(final ConfigureWireMock... annotations) {
5862
this.annotations.addAll(Arrays.asList(annotations));
63+
this.sanityCheckDuplicateNames(this.annotations);
5964
}
6065

6166
void parse(final Class<?> clazz) {
@@ -65,6 +70,19 @@ void parse(final Class<?> clazz) {
6570
}
6671
}
6772

73+
private void sanityCheckDuplicateNames(final List<ConfigureWireMock> check) {
74+
final List<String> names = check.stream().map(it -> it.name()).toList();
75+
final Set<String> dublicateNames =
76+
names.stream()
77+
.filter(it -> Collections.frequency(names, it) > 1)
78+
.collect(Collectors.toSet());
79+
if (!dublicateNames.isEmpty()) {
80+
throw new IllegalStateException(
81+
"Names of mocks must be unique, found duplicates of: "
82+
+ dublicateNames.stream().sorted().collect(Collectors.joining(",")));
83+
}
84+
}
85+
6886
boolean isEmpty() {
6987
return this.annotations.isEmpty();
7088
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.wiremock.spring.test;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.Assert.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.wiremock.spring.ConfigureWireMock;
8+
import org.wiremock.spring.EnableWireMock;
9+
import org.wiremock.spring.internal.WireMockContextCustomizerFactory;
10+
11+
class ConfigurationValidationTest {
12+
13+
@EnableWireMock({@ConfigureWireMock, @ConfigureWireMock})
14+
private static class EnableWireMockSameDefaultName {}
15+
16+
@EnableWireMock({@ConfigureWireMock(name = "w1"), @ConfigureWireMock(name = "w1")})
17+
private static class EnableWireMockSameGivenName {}
18+
19+
@Test
20+
void testDuplicateNames_default() {
21+
final IllegalStateException thrown =
22+
assertThrows(
23+
IllegalStateException.class,
24+
() ->
25+
new WireMockContextCustomizerFactory()
26+
.createContextCustomizer(EnableWireMockSameDefaultName.class, null));
27+
assertThat(thrown.getMessage())
28+
.isEqualTo("Names of mocks must be unique, found duplicates of: wiremock");
29+
}
30+
31+
@Test
32+
void testDuplicateNames_given() {
33+
final IllegalStateException thrown =
34+
assertThrows(
35+
IllegalStateException.class,
36+
() ->
37+
new WireMockContextCustomizerFactory()
38+
.createContextCustomizer(EnableWireMockSameGivenName.class, null));
39+
assertThat(thrown.getMessage())
40+
.isEqualTo("Names of mocks must be unique, found duplicates of: w1");
41+
}
42+
}

0 commit comments

Comments
 (0)