Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk5 min

How to Structure Spring Boot Vertical Slices and Test Each Boundary

A vertical slice groups Spring Boot code around a capability. Learn when to test its business rules, MVC layer, persistence, whole module, or full application.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Spring Boot, a vertical slice groups the code for a business capability—such as placing an order—across the technical layers it needs. Testing that slice is different from using Boot’s technical test slices: @WebMvcTest focuses on MVC, while @DataJpaTest focuses on JPA. For a functional module tested across its layers, Spring Modulith provides @ApplicationModuleTest.

What a vertical slice means in a Spring Boot application

A vertical slice organizes code around a capability or domain module rather than placing all controllers, services, and repositories in application-wide technical folders. The module can contain several technical layers, while exposing a defined interface to other modules.

As an Amazon Associate I earn from qualifying purchases.

Spring Modulith describes an application module in terms of a provided interface (Spring beans and published events available to other modules), internal implementation that other modules should not access, and required interfaces to other modules. By default, its convention treats direct sub-packages of the package containing the main Spring Boot application class as application modules. That is a useful convention, not a requirement for every vertical-slice design.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
com.example.shop
  ShopApplication.java
  orders/
    OrderController.java
    PlaceOrder.java
    OrderRepository.java
    Order.java
  inventory/
    InventoryApi.java
    InventoryService.java
    StockRepository.java

In this example, the orders capability keeps its endpoint, use-case logic, and persistence code together. The inventory module can provide an API or publish events rather than making its internal implementation available to orders. Spring Modulith can help verify module structure and test modules, but it does not prescribe this exact layout. See the Spring Modulith application-module documentation.

Vertical module tests and Boot test slices answer different questions

The word “slice” is used for two different boundaries. Spring Boot test slices narrow the application context around a technical layer. A vertical application module groups functionality that may span several layers. Spring Modulith’s @ApplicationModuleTest can load a module across those layers.

Approach Best question to answer What it loads Important limit
Plain unit test Does this object or business rule behave correctly? Objects constructed directly, without Spring when possible Does not prove Spring wiring or framework integration.
@WebMvcTest Does an MVC endpoint handle requests correctly? MVC infrastructure and selected MVC components, including MockMvc Ordinary components and configuration-properties beans are not scanned by default.
@DataJpaTest Do JPA mappings and repository operations behave correctly? JPA entities, repositories, and database test setup An embedded database may not reproduce production database behavior.
@ApplicationModuleTest Does one functional module work across its layers? The current application module, optionally with dependencies External module beans must be included or replaced when needed.
@SpringBootTest Does broad application wiring or an end-to-end path work? The application context, with an optional mock or real web environment Use the broader scope when the behavior under test requires it.

These scopes are not a hierarchy of test quality. Choose the narrowest boundary that includes the behavior you need to prove. Spring Boot’s testing documentation describes Boot’s test annotations and context setup; Spring Modulith’s testing guide describes module-level testing and combining module and technical boundaries.

Choose the test boundary that matches the behavior

Test business rules without starting Spring

If a rule can be tested by constructing its objects and passing inputs directly, use a plain unit test. It verifies the rule without asserting that Spring can wire the application. Bring in a Spring context when the behavior actually depends on framework integration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use @WebMvcTest for controller behavior

Use this focused test for request mapping, validation, serialization, and MVC exception handling. It configures Spring MVC and MockMvc, while limiting component scanning to MVC-related types. Ordinary @Component and @ConfigurationProperties beans are not scanned by default, so provide required collaborators as test doubles or import only the configuration the test needs. If the application uses Spring Security, use its test support rather than assuming security behavior is absent.

Use @DataJpaTest for persistence behavior

This annotation is for entity mapping and repository behavior. It scans entities and configures Spring Data JPA repositories. When an embedded database is on the test classpath, Boot configures one; tests are transactional and roll back by default. An embedded database is not evidence that vendor-specific production behavior—such as dialect handling or locking—works. Configure the test to use the real database when that behavior matters.

Use @ApplicationModuleTest for a capability spanning layers

Place the test in the module package or one of its sub-packages, add Spring Modulith’s test support, and annotate the test with @ApplicationModuleTest. It boots the module containing the test in an integration-test context, rather than requiring the entire application to be loaded.

Choose a bootstrap mode according to the module’s dependencies:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • STANDALONE: the current module only.
  • DIRECT_DEPENDENCIES: the current module and its direct dependencies.
  • ALL_DEPENDENCIES: the current module and its dependency tree.

If the test cannot start because the module needs a bean from another module, either include the needed module deliberately or replace the external bean with @MockitoBean. A module that requires many beans from other modules may be highly coupled; review whether its boundaries are clear and whether an event-based interaction would fit. Events are a design option, not an automatic improvement.

Combine module and technical scope when only one layer matters

If the question is specifically about the repository layer inside one functional module, Spring Modulith documents combining @ModuleSlicing with a Boot slice such as @DataJpaTest. This applies both boundaries: the functional module and the technical layer. It is distinct from @ApplicationModuleTest, which tests the module across its layers.

Reserve @SpringBootTest for application-wide behavior

@SpringBootTest creates an application context through SpringApplication. By default it does not start a server; its webEnvironment setting can select a mock environment or a real server on a random port. Use a real server when the behavior depends on actual HTTP-server startup or network communication, not simply because the test concerns an endpoint.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What to check when a focused test fails to start

  • A required bean is missing: a test slice intentionally excludes many application components. Add a test double or import the specific configuration required rather than broadening the context immediately.
  • Boot’s slice scans too much or too little: an explicit @ComponentScan on the main @SpringBootApplication can disable the default filters used by test slicing. Consider moving custom scanning to separate configuration or restoring the relevant filters.
  • A module test needs another module: include the necessary dependency through the selected bootstrap mode, or replace the external bean with @MockitoBean. Repeated cross-module requirements can be a reason to examine coupling.
  • A persistence test passes locally but production behavior is uncertain: verify the behavior against the production database when dialect-specific, locking, or other vendor-specific behavior is part of the claim.

Spring’s test framework caches application contexts between tests that share configuration. A context-based test therefore does not necessarily reload the application for every test method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep examples aligned with the Spring Boot version

Spring Boot’s testing modules, package names, and dependency coordinates vary by release. The official reference listed stable documentation versions 4.1.1 and 3.5.16 on October 7, 2026. Select one Boot release for a project and use its matching documentation and dependencies; do not combine snippets from different generations. Spring Modulith’s cited reference pages do not establish a specific release here, so the examples do not claim one.

As the Spring Boot Reference Documentation puts it: “A Spring Boot application is a Spring ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.” The practical question is not whether every test should use that full context, but which context contains the behavior the test is meant to verify.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.