Skip to content

test: verify module boundaries with Spring Modulith - #297

Merged
epugh merged 1 commit into
apache:mainfrom
adityamparikh:feat/spring-modulith-verification
Nov 24, 2026
Merged

epugh merged 1 commit into
apache:mainfrom
adityamparikh:feat/spring-modulith-verification

Conversation

@adityamparikh

Copy link
Copy Markdown
Contributor

Summary

Adds a test that enforces the server's package boundaries, so neither contributors nor AI coding agents can erode them without noticing. ModularityTests runs Spring Modulith's ApplicationModules.of(Main.class).verify():

org.apache.solr.mcp.server          # each direct sub-package is a module
├── collection/  config/  schema/  search/  security/  util/
└── indexing/
    └── documentcreator/            # internal to `indexing`; other modules may not use it

verify() fails on (a) dependency cycles between modules and (b) any module using another module's internal sub-package types. The code already follows this layout: every package has a package-info.java, and there are no cross-module sub-package imports and no cycles. No production code changes. This PR turns an existing convention into a build check.

 gradle/libs.versions.toml
+  spring-modulith = "2.1.3"                      # line built against Boot 4.1
+  spring-modulith-starter-test  -> `test` bundle
 build.gradle.kts  dependencyManagement.imports
+  mavenBom("org.springframework.modulith:spring-modulith-bom:…")
 src/test/java/org/apache/solr/mcp/server/
+  ModularityTests.java    # verify() + PlantUML diagrams to build/spring-modulith-docs (not committed)
 AGENTS.md (CLAUDE.md symlink)
+  "Module boundaries": if it fails, fix the design rather than the test;
+  expose internal packages only explicitly in package-info.java

Why now: more of this code is written with coding agents, which readily take the shortest path, for example importing indexing.documentcreator.* from search. Review catches some of that. A failing test catches all of it, and its message tells the author (human or agent) exactly which dependency to fix.

Test scope only. Modulith is not on productionRuntimeClasspath, so the bootJar, the CycloneDX SBOM, the generated binary LICENSE/NOTICE and the native image are unchanged. The test is @DisabledInNativeImage, because ArchUnit classpath scanning can't run in the nativeTest binary, the same treatment as the Mockito-based tests. It starts no Spring context and needs no Solr, so it runs in the plain unitTest task too. Spring Modulith is Apache-2.0.

Evidence

  • Before: nothing stops a cross-module reach into an internal package. A throwaway search/BoundaryProbe.java taking a JsonDocumentCreator parameter compiles and builds green on main.
    After: the same probe fails the build:
    ModularityTests > verifiesModuleStructure() FAILED
    org.springframework.modulith.core.Violations:
    - Module 'search' depends on non-exposed type
      org.apache.solr.mcp.server.indexing.documentcreator.JsonDocumentCreator within module 'indexing'!
    
  • Without the probe: ./gradlew build passes, 459 tests, 0 failures (ModularityTests: 2 tests, 0 skipped). Spotless, RAT and the binary-licence completeness gate all pass.
  • ./gradlew dependencies --configuration productionRuntimeClasspath | grep -c modulith → 0.
  • Checked against the currently open PRs: none add a sub-package import across modules. feat(indexing): index-url — index a document set from an allow-listed http(s) URL in both transports (#208) #210's indexing.documentcreator import is from inside indexing. A new top-level package (e.g. cluster/ in feat(cluster): return get-cluster-status and list-configsets as records #295) just becomes a new module.

Merge Danger

Door: two-way

Revert the commit to remove it: one test class, one BOM line, one catalog entry, one doc section.

Blast Radius: test-classpath

The only new effect is on future PRs: one that adds a cycle or an internal cross-module dependency now fails test and must restructure or expose the package deliberately via @NamedInterface in package-info.java. The test classpath gains spring-modulith-core/-test/-docs (with ArchUnit and jMolecules), plus spring-data-commons and spring-tx transitively. spring-modulith-test registers one ContextCustomizerFactory, and it only acts on @ApplicationModuleTest classes, of which there are none. The full suite passes unchanged.

🤖 Generated with Claude Code

Add ModularityTests, which runs ApplicationModules.of(Main.class).verify()
so that dependency cycles between the top-level feature packages, and any
use of another module's internal sub-package, fail the build. The test
also writes PlantUML module diagrams to build/spring-modulith-docs.

Spring Modulith 2.1.3 (the line built against Spring Boot 4.1) is added
as a test-only dependency: spring-modulith-bom in dependencyManagement and
spring-modulith-starter-test in the test bundle. Nothing reaches
productionRuntimeClasspath, so the bootJar, SBOM, binary LICENSE/NOTICE
and native image are unchanged. The test is @DisabledInNativeImage since
ArchUnit classpath scanning cannot run in the native test binary.

The current structure already passes with no code changes. AGENTS.md gets
a short rule: if the test fails, fix the dependency instead of weakening
the test, and expose internal packages only explicitly in package-info.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Aditya Parikh <aditya.m.parikh@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

@epugh
epugh merged commit dc2af9f into apache:main Nov 24, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants