Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Spring MockMvc standalone tests: know which HTTP layers were assembled

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

MockMvc standalone setup runs Spring MVC request handling for supplied controllers without loading the application's complete web context.

Download Spring source kit

The controller contract is still useful

The idempotency fixture sends POST requests through MockMvc and checks 400, 200 and 409 responses. It catches mapping, request-header and response mistakes in the controller. It does not load the application's security filter chain, datasource, production codec setup or reverse proxy. The replay lesson states its single-instance state limit.

Use a full web application context or real HTTP test for claims about configured filters, authentication, CSRF, CORS, exception advice and endpoint wiring. Even that still does not test a deployment proxy or external identity provider. Testing boundaries lays out the layers.

Make evidence match the claim

A standalone test can check a controller's response policy quickly. A transaction test should obtain the proxied service from a context. A deployed behavior claim needs a deployment-level test. Run a rejected case as well as the success path; a page that only shows 200 misses the contract's most important branch.

Checked source

Java
var client = MockMvcBuilders.standaloneSetup(new ReceiptIntake()).build();
client.perform(post("/contract/idempotent-receipts")
        .contentType(MediaType.TEXT_PLAIN)
        .header("Idempotency-Key", "intake-41")
        .content("42"))
    .andExpect(status().isOk())
    .andExpect(content().string("receipt-1"));

Verification boundary

IdempotentReceiptPostTest.replayReturnsTheOriginalReceiptAndConflictingPayloadIsRejected runs in the downloadable Spring source kit. The excerpt leaves out surrounding setup and imports; the kit contains the complete test.

Costs and limits

Mock requests avoid a listening server and are fast, but speed is not coverage. The source-kit test checks MVC mapping on one supplied controller; its filter-chain and deployment assumptions need separate tests.

Common Mistakes

  • Do not infer production authentication from a standalone controller test.
  • Do not call an in-memory MockMvc request a real network test.
  • Do not omit the error branch when documenting an HTTP contract.

Read next

Spring MockMvc tests: HTTP behavior without claiming a real network test, Spring tests: separate business rules, wiring and transport, Spring POST idempotency keys: replay the same receipt result.

spring
spring-boot
mockmvc-standalone-scope
Storage details