A real HTTP server handles requests on another thread, so a test-managed transaction cannot be assumed to roll back those writes.
Spring Boot HTTP test: client rollback does not own server writes
Track the thread boundary
A test starts a random-port Spring Boot server and calls POST /receipts. The test method may be annotated @Transactional, but the server processes the request on its own thread and transaction. Rolling back the test thread does not rewind the server's committed receipt. This differs from a repository slice where test and repository usually share the test-managed transaction. Transaction context] is bound to its execution thread.
Own cleanup explicitly
Give each test a unique tenant or receipt key and remove its rows through a controlled cleanup path, or reset an isolated database between tests. Query the database in a fresh transaction after the HTTP response so the assertion sees committed state. A MockMvc test can share more of the test thread, but it does not exercise the network stack; choose it for MVC behavior], not a real port contract.
Test both success and failure
For a command that writes a receipt and outbox event, assert both exist after a successful HTTP response. Then cause a database rejection and assert neither durable row remains. This proves the rollback invariant] through the actual request path. Never rely on a cleanup rollback that runs only in the test thread.
Implementation contract
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureTestRestTemplate
class ReceiptHttpCommitTest {
@Autowired TestRestTemplate client;
@Autowired ReceiptRepository receipts;
String externalKey;
@Test void acceptedReceiptIsCommitted() {
externalKey = "receipt-http-" + UUID.randomUUID();
ResponseEntity<Void> response = client.postForEntity(
"/receipts", new ReceiptRequest(externalKey, 4700), Void.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(receipts.existsByExternalKey(externalKey)).isTrue();
}
@AfterEach void removeCommittedFixture() {
if (externalKey != null)
receipts.findByExternalKey(externalKey).ifPresent(receipts::delete);
}
}Cost and verification
A real server test loads the full context and opens a port. It is slower than a repository or MVC slice, so use it for behavior that crosses HTTP, transaction and persistence boundaries; isolate its committed data deliberately.
Common Mistakes
- Do not expect a test-method rollback to undo a server-thread commit.
- Do not leave committed test rows for later tests to discover.
- Do not claim a MockMvc-only test proved real HTTP listener behavior.
Read next
Spring imperative transaction: a worker thread does not inherit it, Spring command rollback test: inspect state after an injected failure, Spring MockMvc tests: HTTP behavior without claiming a real network test, Spring Data JPA test: flush and clear before trusting a read, Spring TransactionTemplate: roll back a failed multi-row change.
Related test contract
Spring @Sql fixtures: decide whether setup commits outside the test rollback.
Related test contract
Spring WebTestClient target: distinguish mock binding from a running port.
