一键导入
java-springboot-testing
Expert Spring Boot testing specialist that selects the best testing techniques for your situation with JUnit and AssertJ. Trigger: When writing Spring Boot tests, need testing patterns, or working with test slices.
菜单
Expert Spring Boot testing specialist that selects the best testing techniques for your situation with JUnit and AssertJ. Trigger: When writing Spring Boot tests, need testing patterns, or working with test slices.
Pre-built UI component libraries for server-rendered HTML: Preline UI, HyperUI, Flowbite. Modals, tables, forms, navbars, dropdowns — no React, no build step. Trigger: UI components, component library, Preline, HyperUI, Flowbite, Tailwind CSS components, pre-built UI.
Server-side web UI with Spring Boot: Thymeleaf templates, HTMX for dynamic interactions, Alpine.js for client-side behavior. No React, no webpack. Trigger: Thymeleaf, HTMX, Alpine.js, Spring MVC template, server-side rendering, web UI.
REST API design best practices: resource naming, versioning, error handling, pagination, HATEOAS, rate limiting, OpenAPI documentation. Trigger: API design, REST API, endpoint, OpenAPI, RESTful, API versioning, or API documentation.
Docker and containerization best practices: multi-stage builds, docker-compose, networking, volumes, security, and image optimization. Trigger: Docker, Dockerfile, docker-compose, container, image build, or containerization.
General database design principles: modeling, normalization, indexing, naming conventions, migrations, and query optimization. Trigger: Database design, data modeling, schema design, table design, or migration planning.
Comprehensive best practices for developing high-quality Spring Boot applications with production-ready patterns. Trigger: When developing Spring Boot applications, need best practices, or working with Spring framework.
| name | java-springboot-testing |
| description | Expert Spring Boot testing specialist that selects the best testing techniques for your situation with JUnit and AssertJ. Trigger: When writing Spring Boot tests, need testing patterns, or working with test slices. |
| license | Apache-2.0 |
| metadata | {"author":"vekzz-dev","version":"1.1"} |
Testing a controller endpoint?
Yes → @WebMvcTest with MockMvcTester
Testing repository queries?
Yes → @DataJpaTest with Testcontainers (real DB)
Testing business logic in service?
Yes → Plain JUnit + Mockito (no Spring context)
Testing external API client?
Yes → @RestClientTest with MockRestServiceServer
Testing JSON mapping?
Yes → @JsonTest
Need full integration test?
Yes → @SpringBootTest with minimal context config
| Scenario | Annotation | Reference |
|---|---|---|
| Controller + HTTP semantics | @WebMvcTest | references/webmvctest.md |
| Repository + JPA queries | @DataJpaTest | references/datajpatest.md |
| REST client + external APIs | @RestClientTest | references/restclienttest.md |
| JSON (de)serialization | @JsonTest | references/test-slices-overview.md |
| Full application | @SpringBootTest | references/test-slices-overview.md |
# Run all tests
mvn test
# Run specific test class
mvn test -Dtest=OrderControllerTest
# Run tests with specific tag
mvn test -Dgroups=fast
# Run tests with Gradle
gradle test
# Run specific test with Gradle
gradle test --tests OrderControllerTest
# Run integration tests
mvn verify -P integration-test
When a method or class is too complex to test effectively:
Example of refactoring recommendation:
// Before: Complex method hard to test
public Order processOrder(OrderRequest request) {
// Validation, discount calculation, payment, inventory, notification...
// 50+ lines of mixed concerns
}
// After: Refactored into testable units
public Order processOrder(OrderRequest request) {
validateOrder(request);
var order = createOrder(request);
applyDiscount(order);
processPayment(order);
updateInventory(order);
sendNotification(order);
return order;
}
Create helper methods for commonly used objects and mock setup to enhance readability and maintainability.
Use descriptive display names to clarify test intent:
@Test
@DisplayName("Should calculate discount for VIP customer")
void shouldCalculateDiscountForVip() { }
@Test
@DisplayName("Should reject order when customer has insufficient credit")
void shouldRejectOrderForInsufficientCredit() { }
Always structure tests in this order:
Write tests with real production scenarios in mind. This makes tests more relatable and helps understand code behavior in actual production cases.
Aim for 80% code coverage as a practical balance between quality and effort. Higher coverage is beneficial but not the only goal.
Use Jacoco maven plugin for coverage reporting and tracking.
Coverage Rules:
What to Prioritize:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For WebMvc tests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
For up-to-date information on testing with Spring Boot:
Resolve the libraryId - Use context7_resolve-library-id:
libraryName: "junit 5", "assertj", "mockito", or "spring boot test"query: what you need (e.g., "mocking beans", "assertions best practices")Query the docs - Use context7_query-docs:
libraryId: "/junit/junit5", "/assertj/assertj", or "/spring/spring-boot"query: your specific question about testingBefore writing tests, consult Context7 to get updated examples of the library you're going to use.