Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

In Spring Modulith, how are application modules defined, and how do you write a test that fails the build when one module uses another module's internal classes?

By default, Spring Modulith treats each direct sub-package of the main application package, such as com.acme.orders and com.acme.billing, as an application module, and classes in that package form its API while classes in its sub-packages are internal. A plain JUnit test that calls ApplicationModules.of(Application.class).verify() fails the build when a module uses another module's internal classes or when modules depend on each other in a cycle.

Why this question is asked

In a large Spring Boot application, package boundaries tend to erode. Someone in billing needs an order detail, imports a repository from the orders package, and the two areas are now tied together. Nothing in plain Java stops it, because public classes are visible everywhere. Spring Modulith makes those boundaries explicit and testable. Interviewers ask this to see whether we know how modules are detected and how to turn the rules into a failing test. In this answer, we will recap how modules are defined, write the verification test, break a rule and fix it, control what a module exposes and depends on, and see documentation and module-level integration tests.

1How Spring Modulith Defines a Module

Spring Modulith looks at the package of the class annotated with @SpringBootApplication, here com.acme. Each direct sub-package of it is an application module. The types in the module's base package, such as com.acme.orders.OrderService, are its API and may be used by other modules. Types in sub-packages, such as com.acme.orders.internal.OrderRepository, are internal to the module, even if they are declared public in Java.

Notice that this needs no configuration. The convention is the default, and many applications already follow it. Spring Modulith uses ArchUnit, a library that analyses compiled classes, to read the dependencies between types.

Packages and what Spring Modulith makes of them
PackageRoleOther modules may use it?
com.acmeapplication rootnot a module
com.acme.ordersorders module APIyes
com.acme.orders.internalorders internalsno
com.acme.billingbilling module APIyes
com.acme.billing.internalbilling internalsno

2Writing the Verification Test

The test is a plain JUnit test; it does not start a Spring context, so it runs in a second or two. ApplicationModules.of reads the module structure from the main class, and verify() checks the rules and throws an exception that lists every violation.

Java
import org.springframework.modulith.core.ApplicationModules;
class ModularityTests {
ApplicationModules modules = ApplicationModules.of(Application.class);
@Test
void verifiesModularStructure() {
modules.verify(); // passes when no module uses another's internals and there are no cycles
}
}

Because it is an ordinary test, it runs in every CI build. The first time we add it to an existing application, it often fails with a list of violations; that list is a useful map of where the boundaries have already eroded.

3Breaking a Rule and Seeing the Failure

Let us break the rule. A billing class uses the orders module's internal repository directly.

Java
package com.acme.billing;
import com.acme.orders.internal.OrderRepository; // internal type of another module
@Service
class InvoiceService {
private final OrderRepository orders;
InvoiceService(OrderRepository orders) { this.orders = orders; }
}
// ModularityTests.verifiesModularStructure()
// org.springframework.modulith.core.Violations:
// - Module 'billing' depends on non-exposed type com.acme.orders.internal.OrderRepository within module 'orders'!
// (wording may differ between versions)

The fix is to depend on the orders module's API instead. We add a public method to com.acme.orders.OrderService, or a small query interface in the base package, that returns what billing needs, and InvoiceService calls that. The test passes again, and orders is free to change its repository later without breaking billing. The same test also fails if orders and billing depend on each other in a cycle, which forces us to choose a direction or to communicate through application events.

4Controlling What a Module Exposes and Depends On

Sometimes a module wants to expose more than its base package, for example an spi package that other modules implement. We mark that package with @NamedInterface in its package-info.java, and other modules may then use it. We can also restrict which modules a module may depend on with @ApplicationModule on the module's base package.

Java
// src/main/java/com/acme/orders/spi/package-info.java
@org.springframework.modulith.NamedInterface("spi")
package com.acme.orders.spi;
// src/main/java/com/acme/billing/package-info.java
@org.springframework.modulith.ApplicationModule(allowedDependencies = { "orders", "orders :: spi" })
package com.acme.billing;
// billing -> inventory now fails verify(): not an allowed dependency

So the rules are explicit and live in the code. allowedDependencies names modules, or a module's named interface with the double colon syntax. When billing later imports something from inventory, verify() fails with a message that the dependency is not allowed, and the change has to be discussed instead of slipping in.

5Documentation and Module Tests

The same model can produce documentation. Documenter writes diagrams of the modules and their dependencies, and a module canvas per module listing its API, events and beans, to the build output folder. Running it in a test keeps the documentation in step with the code.

For integration tests, @ApplicationModuleTest on a test class inside a module's package starts a Spring context with only that module's beans, by default, or with its dependencies when we choose a bootstrap mode that includes them. Beans from other modules that the module needs can be replaced with @MockitoBean. This keeps tests fast and proves the module works on its own.

Java
@Test
void writesDocumentation() {
new Documenter(modules).writeDocumentation(); // diagrams and canvases in the build output folder
}
@ApplicationModuleTest // in package com.acme.orders
class OrderModuleTests {
@Autowired OrderService orders;
@Test void placesOrder() { assertThat(orders.place(cart()).status()).isEqualTo(PLACED); }
}

6Follow-up questions

How would you split a module that has grown too large?

First look at its structure in the generated diagrams and at which of its parts change together. If it contains two areas with few links between them, such as order capture and order fulfilment, create a new top-level package for one of them, move its classes, give it a small API in its base package and replace direct calls with that API or with application events. Then run verify() to make sure no internal dependencies remain, and use @ApplicationModuleTest to prove each module starts on its own. Doing it in small moves, one class group at a time, keeps the test green throughout.

Does the verification test slow the build?

Very little. It analyses compiled classes without starting Spring, so it usually takes a second or two even for large applications.

Can I keep a module's internals in the base package?

Technically yes, but then they are part of the API. If a class should not be used from outside, move it into a sub-package or reduce its visibility to package-private, which plain Java already enforces.

7Conclusion

In this answer, we saw that Spring Modulith treats each direct sub-package of the application package as a module, with the base package as its API and sub-packages as internal. ApplicationModules.of(Application.class).verify() in a plain JUnit test fails on access to internal types and on cycles. @NamedInterface exposes extra packages, @ApplicationModule(allowedDependencies) restricts dependencies, Documenter generates diagrams and canvases, and @ApplicationModuleTest boots one module for integration tests.

What to say in the interviewAbout 70 seconds aloud

Open with

Spring Modulith detects modules from the package structure. The package of the Spring Boot application class is the root, and each direct sub-package, like com.acme.orders and com.acme.billing, is an application module.

Then, if they want more

Types in the module's base package are its API, and anything in a sub-package, like orders.internal, is internal, even if it is public in Java. To enforce that, I write a plain JUnit test with no Spring context: ApplicationModules modules = ApplicationModules.of(Application.class), then modules.verify().

It throws a Violations exception that lists each problem, for example billing depending on a non-exposed type inside orders, and it also fails on dependency cycles between modules. It runs in a second or two, so it can run in every build.

If a module needs to expose more than its base package, I annotate that package with @NamedInterface in package-info.java, and I can restrict a module's dependencies with @ApplicationModule and allowedDependencies on its base package. From the same model, new Documenter(modules).writeDocumentation() generates module diagrams and canvases, and for integration tests @ApplicationModuleTest boots only one module, with other modules' beans mocked.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Spring Modulith, how are application modules defined, and how do you write a test...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/7379eeb05c/