Files

2.4 KiB

spring-test-slices

Companion code for the ankurm.com post Spring Boot 4 Test Slices: @WebMvcTest, @JsonTest, @RestClientTest and @DataJpaTest Compared.

A small book-catalogue app (controller, service, JPA repository, a RestClient client for an ISBN service, a Jackson 3 @JacksonComponent) and one test class per slice, plus the same app under a full @SpringBootTest as the baseline. Every test writes the real output to output/ and asserts the same facts, so the numbers in the post fail the build if they stop being true.

Tool Version Source
Spring Boot 4.1.2 maven-metadata.xml (latest GA when this was written; 4.2.0 is not GA yet)
Spring Framework 7.0.10 resolved by the Boot BOM
Hibernate ORM 7.4.13.Final resolved by the Boot BOM
JDK 25

Run

./scripts/run-all.sh      # about a minute; regenerates everything in output/
mvn test                  # just the suite

Startup times (output/30-startup-times.txt) vary by machine and by run: treat them as indicative.

Files

  • src/main/java/lab/slices/ the app: BookController, ApiExceptionHandler, BookService, BookRepository, IsbnLookupClient, PriceJson (@JacksonComponent), AuditLogger (a plain @Component, the canary)
  • src/test/java/lab/slices/ WebMvcSliceTest, JsonSliceTest, RestClientSliceTest, DataJpaSliceTest, FullContextTest, PackageMovesTest, OldImportsCompileTest, FailureCaptureTest, ZMatrixTest
  • src/test/java/lab/slices/failing/ test classes that fail on purpose (tag failure-demo, excluded from the normal run; FailureCaptureTest runs them through the JUnit Launcher and records the real exception)
  • src/broken/ Boot 3 style tests that no longer compile (not part of the Maven build; OldImportsCompileTest hands them to javac)
  • output/ captured real output of the last run
Output What it shows
01 to 06 one transcript per slice, plus the full context: requests, responses, and which beans exist
07, 08 the exceptions a missing mock, a missing scan and @EnableJpaAuditing produce
09 where every test annotation lives in Boot 4.1.2, asked of the classpath
10 javac's own errors for Boot 3 imports
20 the bean-presence table, one column per slice
11, 12 which Jackson each JSON tester wraps; which test jars spring-boot-starter-test alone brings in
30 context startup time per slice (indicative)