Skip to content

Commit 1dbce34

Browse files
odrotbohmciberkleid
authored andcommitted
GH-757 - Fix test context cache key contributions.
Both ModuleContextCustomizer and ModuleTypeExcludeFilter contribute to the calculation of the context configuration which the Spring Test Context Framework uses to decide whether it's necessary to create new ApplicationContext instances. Both of them previously used a Supplier<ModuleTestExecution> to calculate equals(…) and hashCode() which -- by definition -- does not result in the same result even when created with identical input. We now rather use the source class instance eventually backing the ModuleTestExecution, as that is the internal cache key in turn.
1 parent 0ba371f commit 1dbce34

File tree

4 files changed

+104
-17
lines changed

4 files changed

+104
-17
lines changed

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ static class ModuleContextCustomizer implements ContextCustomizer {
6262
private static final Logger LOGGER = LoggerFactory.getLogger(ModuleContextCustomizer.class);
6363

6464
private final Supplier<ModuleTestExecution> execution;
65+
private final Class<?> source;
66+
67+
ModuleContextCustomizer(Class<?> testClass) {
6568

66-
private ModuleContextCustomizer(Class<?> testClass) {
6769
this.execution = ModuleTestExecution.of(testClass);
70+
this.source = testClass;
6871
}
6972

7073
/*
@@ -143,24 +146,12 @@ private static void logModules(ModuleTestExecution execution) {
143146
LOGGER.info("");
144147
}
145148

146-
private static void logHeadline(String headline) {
147-
logHeadline(headline, () -> {});
148-
}
149-
150-
private static void logHeadline(String headline, Runnable additional) {
151-
152-
LOGGER.info("");
153-
LOGGER.info(headline);
154-
additional.run();
155-
}
156-
157149
/*
158150
* (non-Javadoc)
159151
* @see java.lang.Object#equals(java.lang.Object)
160152
*/
161153
@Override
162154
public boolean equals(Object obj) {
163-
164155
if (this == obj) {
165156
return true;
166157
}
@@ -169,7 +160,7 @@ public boolean equals(Object obj) {
169160
return false;
170161
}
171162

172-
return Objects.equals(execution, that.execution);
163+
return Objects.equals(this.source, that.source);
173164
}
174165

175166
/*
@@ -178,7 +169,18 @@ public boolean equals(Object obj) {
178169
*/
179170
@Override
180171
public int hashCode() {
181-
return Objects.hash(execution);
172+
return Objects.hashCode(source);
173+
}
174+
175+
private static void logHeadline(String headline) {
176+
logHeadline(headline, () -> {});
177+
}
178+
179+
private static void logHeadline(String headline, Runnable additional) {
180+
181+
LOGGER.info("");
182+
LOGGER.info(headline);
183+
additional.run();
182184
}
183185
}
184186

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTypeExcludeFilter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@
2222
import org.springframework.boot.context.TypeExcludeFilter;
2323
import org.springframework.core.type.classreading.MetadataReader;
2424
import org.springframework.core.type.classreading.MetadataReaderFactory;
25+
import org.springframework.util.Assert;
2526

2627
/**
2728
* @author Oliver Drotbohm
2829
*/
2930
class ModuleTypeExcludeFilter extends TypeExcludeFilter {
3031

3132
private final Supplier<ModuleTestExecution> execution;
33+
private final Class<?> source;
3234

3335
public ModuleTypeExcludeFilter(Class<?> testClass) {
36+
37+
Assert.notNull(testClass, "Test class must not be null!");
38+
3439
this.execution = ModuleTestExecution.of(testClass);
40+
this.source = testClass;
3541
}
3642

3743
/*
@@ -58,7 +64,7 @@ public boolean equals(Object obj) {
5864
return false;
5965
}
6066

61-
return Objects.equals(execution, that.execution);
67+
return Objects.equals(source, that.source);
6268
}
6369

6470
/*
@@ -67,6 +73,6 @@ public boolean equals(Object obj) {
6773
*/
6874
@Override
6975
public int hashCode() {
70-
return Objects.hash(execution);
76+
return Objects.hash(source);
7177
}
7278
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.test;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.modulith.test.ModuleContextCustomizerFactory.ModuleContextCustomizer;
22+
23+
/**
24+
* Unit tests for {@link ModuleTypeExcludeFilter}.
25+
*
26+
* @author Oliver Drotbohm
27+
*/
28+
class ModuleContextCustomizerUnitTests {
29+
30+
@Test
31+
void instancesForSameTargetTypeAreEqual() {
32+
33+
var left = new ModuleContextCustomizer(Object.class);
34+
var right = new ModuleContextCustomizer(Object.class);
35+
36+
assertThat(left).isEqualTo(right);
37+
assertThat(right).isEqualTo(left);
38+
assertThat(left).hasSameHashCodeAs(right);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.test;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* Unit tests for {@link ModuleTypeExcludeFilter}.
24+
*
25+
* @author Oliver Drotbohm
26+
*/
27+
class ModuleTypeExcludeFilterUnitTests {
28+
29+
@Test
30+
void instancesForSameTargetTypeAreEqual() {
31+
32+
var left = new ModuleTypeExcludeFilter(Object.class);
33+
var right = new ModuleTypeExcludeFilter(Object.class);
34+
35+
assertThat(left).isEqualTo(right);
36+
assertThat(right).isEqualTo(left);
37+
assertThat(left).hasSameHashCodeAs(right);
38+
}
39+
}

0 commit comments

Comments
 (0)