Skip to content

Commit 8cf3920

Browse files
Lukas Dohmenlukasdodavidbilge
authored andcommitted
GH-31 - Support for optimized test execution.
Initital prototype to support optimized test execution based on the Spring Modulith application module model. The change introduces a new artifact spring-modulith-junit that extends JUnit's test execution lifecycle. It obtains the ApplicationModules model for the application and potentially skips test classes for execution in case the changes made to the application reside in modules the current test case's module does not depend on. Co-authored-by: Lukas Dohmen <l.dohmen@yahoo.de> Co-authored-by: David Bilge <david.bilge@gmail.com>
1 parent db4ab8f commit 8cf3920

File tree

19 files changed

+630
-13
lines changed

19 files changed

+630
-13
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
<module>spring-modulith-runtime</module>
3030
<module>spring-modulith-starters</module>
3131
<module>spring-modulith-test</module>
32-
</modules>
32+
<module>spring-modulith-junit</module>
33+
</modules>
3334

3435
<properties>
3536

spring-modulith-examples/spring-modulith-example-full/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@
6363
<artifactId>spring-boot-configuration-processor</artifactId>
6464
<optional>true</optional>
6565
</dependency>
66-
66+
<dependency>
67+
<groupId>org.springframework.modulith</groupId>
68+
<artifactId>spring-modulith-junit</artifactId>
69+
<version>1.3.0-SNAPSHOT</version>
70+
<scope>test</scope>
71+
</dependency>
6772
</dependencies>
6873

6974
</project>

spring-modulith-examples/spring-modulith-example-full/src/test/java/example/order/OrderIntegrationTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package example.order;
1717

1818
import lombok.RequiredArgsConstructor;
19-
2019
import org.junit.jupiter.api.Test;
2120
import org.springframework.modulith.test.ApplicationModuleTest;
2221
import org.springframework.modulith.test.Scenario;
@@ -28,16 +27,16 @@
2827
@RequiredArgsConstructor
2928
class OrderIntegrationTests {
3029

31-
private final OrderManagement orders;
32-
33-
@Test
34-
void publishesOrderCompletion(Scenario scenario) {
30+
private final OrderManagement orders;
3531

36-
var reference = new Order();
32+
@Test
33+
void publishesOrderCompletion(Scenario scenario) {
34+
// this is a change
35+
var reference = new Order();
3736

38-
scenario.stimulate(() -> orders.complete(reference))
39-
.andWaitForEventOfType(OrderCompleted.class)
40-
.matchingMappedValue(OrderCompleted::orderId, reference.getId())
41-
.toArrive();
42-
}
37+
scenario.stimulate(() -> orders.complete(reference))
38+
.andWaitForEventOfType(OrderCompleted.class)
39+
.matchingMappedValue(OrderCompleted::orderId, reference.getId())
40+
.toArrive();
41+
}
4342
}

spring-modulith-junit/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.modulith</groupId>
8+
<artifactId>spring-modulith</artifactId>
9+
<version>1.3.0-SNAPSHOT</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<name>Spring Modulith - Test Junit</name>
14+
15+
<artifactId>spring-modulith-junit</artifactId>
16+
17+
<properties>
18+
<module.name>org.springframework.modulith.junit</module.name>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.eclipse.jgit</groupId>
24+
<artifactId>org.eclipse.jgit</artifactId>
25+
<version>6.8.0.202311291450-r</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.modulith</groupId>
29+
<artifactId>spring-modulith-core</artifactId>
30+
<version>${project.version}</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-api</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-test</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-autoconfigure</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.mockito</groupId>
46+
<artifactId>mockito-junit-jupiter</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.assertj</groupId>
51+
<artifactId>assertj-core</artifactId>
52+
<scope>test</scope>
53+
</dependency>
54+
</dependencies>
55+
56+
57+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.springframework.modulith.junit;
2+
3+
sealed interface Change {
4+
record JavaClassChange(String fullyQualifiedClassName) implements Change {}
5+
6+
record JavaTestClassChange(String fullyQualifiedClassName) implements Change {}
7+
8+
record OtherFileChange(String path) implements Change {}
9+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.springframework.modulith.junit;
2+
3+
import java.util.Set;
4+
import java.util.stream.Collectors;
5+
6+
import org.springframework.modulith.junit.Change.JavaClassChange;
7+
import org.springframework.modulith.junit.Change.JavaTestClassChange;
8+
import org.springframework.modulith.junit.Change.OtherFileChange;
9+
import org.springframework.modulith.junit.diff.ModifiedFilePath;
10+
import org.springframework.util.ClassUtils;
11+
import org.springframework.util.StringUtils;
12+
13+
final class Changes {
14+
private static final String STANDARD_SOURCE_DIRECTORY = "src/main/java";
15+
private static final String STANDARD_TEST_SOURCE_DIRECTORY = "src/test/java";
16+
17+
private Changes() {}
18+
19+
static Set<Change> toChanges(Set<ModifiedFilePath> modifiedFilePaths) {
20+
return modifiedFilePaths.stream().map(Changes::toChange).collect(Collectors.toSet());
21+
}
22+
23+
static Change toChange(ModifiedFilePath modifiedFilePath) {
24+
if ("java".equalsIgnoreCase(StringUtils.getFilenameExtension(modifiedFilePath.path()))) {
25+
String withoutExtension = StringUtils.stripFilenameExtension(modifiedFilePath.path());
26+
27+
int startOfMainDir = withoutExtension.indexOf(STANDARD_SOURCE_DIRECTORY);
28+
int startOfTestDir = withoutExtension.indexOf(STANDARD_TEST_SOURCE_DIRECTORY);
29+
30+
if (startOfTestDir > 0 && (startOfMainDir < 0 || startOfTestDir < startOfMainDir)) {
31+
String fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(
32+
withoutExtension.substring(startOfTestDir + STANDARD_TEST_SOURCE_DIRECTORY.length() + 1));
33+
34+
return new JavaTestClassChange(fullyQualifiedClassName);
35+
} else if (startOfMainDir > 0 && (startOfTestDir < 0 || startOfMainDir < startOfTestDir)) {
36+
String fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(
37+
withoutExtension.substring(startOfMainDir + STANDARD_SOURCE_DIRECTORY.length() + 1));
38+
39+
return new JavaClassChange(fullyQualifiedClassName);
40+
} else {
41+
// This is unusual, fall back to just assume that the full path is the package -> TODO At least log this
42+
String fullyQualifiedClassName = ClassUtils.convertResourcePathToClassName(withoutExtension);
43+
44+
return new JavaClassChange(fullyQualifiedClassName);
45+
}
46+
} else {
47+
// TODO Do these need to be relative to the module root (i.e. where src/main/java etc. reside)?
48+
return new OtherFileChange(modifiedFilePath.path());
49+
}
50+
}
51+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package org.springframework.modulith.junit;
2+
3+
import java.util.HashSet;
4+
import java.util.Optional;
5+
import java.util.Set;
6+
import java.util.stream.Collectors;
7+
8+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
9+
import org.junit.jupiter.api.extension.ExecutionCondition;
10+
import org.junit.jupiter.api.extension.ExtensionContext;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.boot.autoconfigure.SpringBootApplication;
14+
import org.springframework.boot.test.context.AnnotatedClassFinder;
15+
import org.springframework.modulith.core.ApplicationModule;
16+
import org.springframework.modulith.core.ApplicationModuleDependency;
17+
import org.springframework.modulith.core.ApplicationModules;
18+
import org.springframework.util.ClassUtils;
19+
20+
import com.tngtech.archunit.core.domain.JavaClass;
21+
22+
// add logging to explain what happens (and why)
23+
24+
/**
25+
* Junit Extension to skip test execution if no changes happened in the module that the test belongs to.
26+
*
27+
* @author Lukas Dohmen, David Bilge
28+
*/
29+
public class ModulithExecutionExtension implements ExecutionCondition {
30+
public static final String CONFIG_PROPERTY_PREFIX = "spring.modulith.test";
31+
final AnnotatedClassFinder spaClassFinder = new AnnotatedClassFinder(SpringBootApplication.class);
32+
private static final Logger log = LoggerFactory.getLogger(ModulithExecutionExtension.class);
33+
34+
@Override
35+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
36+
if (context.getTestMethod().isPresent()) {
37+
// Is there something similar liken @TestInstance(TestInstance.Lifecycle.PER_CLASS) for Extensions?
38+
return ConditionEvaluationResult.enabled("Enabled, only evaluating per class");
39+
}
40+
41+
StateStore stateStore = new StateStore(context);
42+
Set<Class<?>> changedClasses = stateStore.getChangedClasses();
43+
if (changedClasses.isEmpty()) {
44+
log.trace("No class changes found, running tests");
45+
return ConditionEvaluationResult.enabled("ModulithExecutionExtension: No changes detected");
46+
}
47+
48+
log.trace("Found following changed classes {}", changedClasses);
49+
50+
Optional<Class<?>> testClass = context.getTestClass();
51+
if (testClass.isPresent()) {
52+
if (changedClasses.contains(testClass.get())) {
53+
return ConditionEvaluationResult.enabled("ModulithExecutionExtension: Change in test class detected");
54+
}
55+
Class<?> mainClass = this.spaClassFinder.findFromClass(testClass.get());
56+
57+
if (mainClass == null) {// TODO:: Try with @ApplicationModuleTest -> main class
58+
return ConditionEvaluationResult.enabled(
59+
"ModulithExecutionExtension: Unable to locate SpringBootApplication Class");
60+
}
61+
ApplicationModules applicationModules = ApplicationModules.of(mainClass);
62+
63+
String packageName = ClassUtils.getPackageName(testClass.get());
64+
65+
// always run test if one of whitelisted files is modified (ant matching)
66+
Optional<ApplicationModule> optionalApplicationModule = applicationModules.getModuleForPackage(packageName);
67+
if (optionalApplicationModule.isPresent()) {
68+
69+
Set<JavaClass> dependentClasses = getAllDependentClasses(optionalApplicationModule.get(),
70+
applicationModules);
71+
72+
for (Class<?> changedClass : changedClasses) {
73+
74+
if (optionalApplicationModule.get().contains(changedClass)) {
75+
return ConditionEvaluationResult.enabled(
76+
"ModulithExecutionExtension: Changes in module detected, Executing tests");
77+
}
78+
79+
if (dependentClasses.stream()
80+
.anyMatch(applicationModule -> applicationModule.isEquivalentTo(changedClass))) {
81+
return ConditionEvaluationResult.enabled(
82+
"ModulithExecutionExtension: Changes in dependent module detected, Executing tests");
83+
}
84+
}
85+
}
86+
}
87+
88+
return ConditionEvaluationResult.disabled(
89+
"ModulithExtension: No Changes detected in current module, executing tests");
90+
}
91+
92+
private Set<JavaClass> getAllDependentClasses(ApplicationModule applicationModule,
93+
ApplicationModules applicationModules) {
94+
95+
Set<ApplicationModule> dependentModules = new HashSet<>();
96+
dependentModules.add(applicationModule);
97+
this.getDependentModules(applicationModule, applicationModules, dependentModules);
98+
99+
return dependentModules.stream()
100+
.map(appModule -> appModule.getDependencies(applicationModules))
101+
.flatMap(applicationModuleDependencies -> applicationModuleDependencies.stream()
102+
.map(ApplicationModuleDependency::getTargetType))
103+
.collect(Collectors.toSet());
104+
}
105+
106+
private void getDependentModules(ApplicationModule applicationModule, ApplicationModules applicationModules,
107+
Set<ApplicationModule> modules) {
108+
109+
Set<ApplicationModule> applicationModuleDependencies = applicationModule.getDependencies(applicationModules)
110+
.stream()
111+
.map(ApplicationModuleDependency::getTargetModule)
112+
.collect(Collectors.toSet());
113+
114+
modules.addAll(applicationModuleDependencies);
115+
if (!applicationModuleDependencies.isEmpty()) {
116+
for (ApplicationModule applicationModuleDependency : applicationModuleDependencies) {
117+
this.getDependentModules(applicationModuleDependency, applicationModules, modules);
118+
}
119+
}
120+
}
121+
122+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.springframework.modulith.junit;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.junit.jupiter.api.extension.ExtensionContext;
7+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
11+
import org.springframework.core.env.StandardEnvironment;
12+
import org.springframework.modulith.junit.Change.JavaClassChange;
13+
import org.springframework.modulith.junit.Change.JavaTestClassChange;
14+
import org.springframework.modulith.junit.Change.OtherFileChange;
15+
import org.springframework.modulith.junit.diff.FileModificationDetector;
16+
import org.springframework.modulith.junit.diff.ModifiedFilePath;
17+
import org.springframework.util.ClassUtils;
18+
19+
class StateStore {
20+
private static final Logger log = LoggerFactory.getLogger(StateStore.class);
21+
22+
private final ExtensionContext.Store store;
23+
24+
StateStore(ExtensionContext context) {
25+
store = context.getRoot().getStore(Namespace.create(ModulithExecutionExtension.class));
26+
}
27+
28+
Set<Class<?>> getChangedClasses() {
29+
// noinspection unchecked
30+
return (Set<Class<?>>) store.getOrComputeIfAbsent("changed-files", s -> {
31+
var environment = new StandardEnvironment();
32+
ConfigDataEnvironmentPostProcessor.applyTo(environment);
33+
34+
var detector = FileModificationDetector.loadFileModificationDetector(environment);
35+
try {
36+
Set<ModifiedFilePath> modifiedFiles = detector.getModifiedFiles(environment);
37+
Set<Change> changes = Changes.toChanges(modifiedFiles);
38+
return toChangedClasses(changes);
39+
} catch (Exception e) {
40+
log.error("ModulithExecutionExtension: Unable to fetch changed files, executing all tests", e);
41+
return Set.of();
42+
}
43+
});
44+
}
45+
46+
private static Set<Class<?>> toChangedClasses(Set<Change> changes) {
47+
Set<Class<?>> changedClasses = new HashSet<>();
48+
for (Change change : changes) {
49+
if (change instanceof OtherFileChange) {
50+
continue;
51+
}
52+
53+
String className;
54+
if (change instanceof JavaClassChange jcc) {
55+
className = jcc.fullyQualifiedClassName();
56+
} else if (change instanceof JavaTestClassChange jtcc) {
57+
className = jtcc.fullyQualifiedClassName();
58+
} else {
59+
throw new IllegalStateException("Unexpected change type: " + change.getClass());
60+
}
61+
62+
try {
63+
Class<?> aClass = ClassUtils.forName(className, null);
64+
changedClasses.add(aClass);
65+
} catch (ClassNotFoundException e) {
66+
log.trace("ModulithExecutionExtension: Unable to find class \"{}\"", className);
67+
}
68+
}
69+
return changedClasses;
70+
}
71+
}

0 commit comments

Comments
 (0)