Skip to content

Commit f4b2b56

Browse files
committed
GH-1395 - Fall back to enabled tests for multiple main classes present.
1 parent 2a497e9 commit f4b2b56

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

spring-modulith-junit/src/main/java/org/springframework/modulith/junit/TestExecutionCondition.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.slf4j.LoggerFactory;
2525
import org.springframework.boot.SpringBootConfiguration;
2626
import org.springframework.boot.test.context.AnnotatedClassFinder;
27+
import org.springframework.lang.Nullable;
2728
import org.springframework.modulith.core.ApplicationModule;
2829
import org.springframework.modulith.core.ApplicationModuleDependencies;
2930
import org.springframework.modulith.core.ApplicationModules;
@@ -71,7 +72,7 @@ ConditionEvaluationResult evaluate(ConditionContext context) {
7172
return enabled("Change in test class detected, executing test.");
7273
}
7374

74-
var mainClass = SPA_CLASS_FINDER.findFromClass(testClass);
75+
var mainClass = findMainClass(testClass);
7576

7677
if (mainClass == null) {// TODO:: Try with @ApplicationModuleTest -> main class
7778
return enabled("Unable to locate SpringBootApplication Class");
@@ -122,5 +123,19 @@ private static ConditionEvaluationResult result(Function<String, ConditionEvalua
122123
return creator.apply(message);
123124
}
124125

126+
/**
127+
* Finds the canonical main class of the application. Falls back to {@literal null} in case multiple types are found.
128+
*
129+
* @param testClass must not be {@literal null}.
130+
*/
131+
private static @Nullable Class<?> findMainClass(Class<?> testClass) {
132+
133+
try {
134+
return SPA_CLASS_FINDER.findFromClass(testClass);
135+
} catch (IllegalStateException o_O) {
136+
return null;
137+
}
138+
}
139+
125140
record ConditionContext(Class<?> testClass, Changes changes) {}
126141
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 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.junit;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.stream.Stream;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.boot.SpringBootConfiguration;
24+
import org.springframework.modulith.junit.diff.ModifiedFile;
25+
26+
/**
27+
* Unit tests for {@link TestExecutionCondition}.
28+
*
29+
* @author Oliver Drotbohm
30+
*/
31+
class TestExecutionConditionUnitTests {
32+
33+
@Test // GH-1391
34+
void fallsBackToEnabledTestIfMultipleMainClassesFound() {
35+
36+
var changes = Changes.of(Stream.of(new ModifiedFile("Foo.java")));
37+
var ctx = new TestExecutionCondition.ConditionContext(getClass(), changes);
38+
39+
assertThat(new TestExecutionCondition().evaluate(ctx).isDisabled()).isFalse();
40+
}
41+
42+
@SpringBootConfiguration
43+
static class First {}
44+
45+
@SpringBootConfiguration
46+
static class Second {}
47+
}

0 commit comments

Comments
 (0)