a-pavithraa/springboot-skills-marketplace / plugins/springboot-architecture/skills/writing-springboot-tests
writing-springboot-tests
VerifiedUse when writing or adding tests or integration tests for Spring Boot code at a service's edges: HTTP clients that call other services (WireMock: 4xx and 5xx responses, timeouts, retries), Kafka or RabbitMQ listeners (redelivery, poison messages, dead-letter topics), events published to other services, or full-API tests against real databases with Testcontainers; domain logic whose rules must hold for every input (calculations, value objects, state transitions, parsing and formatting), tested as properties over generated inputs; also when a Boot 4 test needs the right imports, slices or container wiring. Supplies verified Boot 4.1 templates and the framework traps that make such tests pass without proving anything. Not for reviewing existing tests (see code-reviewer) or Boot 3 to 4 test migration (see springboot-migration).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.9 KB |
| assets/api-integration-test.java | other | 5.1 KB |
| assets/event-publication-test.java | other | 6.2 KB |
| assets/http-client-wiremock-test.java | other | 6.9 KB |
| assets/message-listener-test.java | other | 6.6 KB |
| assets/personas-db.java | other | 1.8 KB |
| assets/personas-jwt.java | other | 2.3 KB |
| assets/personas-oidc.java | other | 2.9 KB |
| assets/pitest-plugin.xml | config | 1.2 KB |
| assets/property-test.java | other | 4.6 KB |
| assets/repository-slice-test.java | other | 2.6 KB |
| assets/route-access-test.java | other | 4.4 KB |
| assets/service-unit-test.java | other | 5.4 KB |
| assets/testcontainers-configuration.java | other | 2.4 KB |
| assets/webmvc-slice-test.java | other | 4.3 KB |
assets/property-test.java
View on GitHubpackage {{PACKAGE}}.orders.domain;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.SplittableRandom;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Property tests for domain logic: each test states a rule that holds for every input, then checks it on generated
* inputs. Plain JUnit, no library: jqwik tells AI agents in its output to ignore its results, and its engine targets
* JUnit Platform 1.x while Boot 4 manages 6.x.
*
* Properties, written down before the code (from the requirement, not from the code):
* - invariant: after any sequence of added lines, the order total is the sum of quantity x unit price, never negative
* - round trip: an order number printed and parsed back is the same order number
* - idempotence: cancelling a cancelled order changes nothing
*/
class OrderPropertiesTest {
// A fixed seed makes every run see the same inputs, so a failure reproduces. Change it to explore new inputs;
// keep the failing case as an example test once it's fixed.
static final long SEED = 20261104L;
static final int CASES = 500;
record Line(String product, int quantity, BigDecimal unitPrice) {}
// Random values rarely hit boundaries, so the generator mixes them in on purpose: 1, the largest quantity the
// rules allow, zero-priced lines, prices with the most decimals the currency allows.
static List<Line> lines(SplittableRandom random) {
int count = random.nextInt(0, 20);
List<Line> lines = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
int quantity = switch (random.nextInt(4)) {
case 0 -> 1;
case 1 -> Order.MAX_QUANTITY;
default -> random.nextInt(1, Order.MAX_QUANTITY + 1);
};
BigDecimal price = random.nextInt(10) == 0
? BigDecimal.ZERO
: BigDecimal.valueOf(random.nextLong(1, 1_000_000), 2);
lines.add(new Line("P-" + random.nextInt(50), quantity, price));
}
return lines;
}
static Stream<List<Line>> orders() {
var random = new SplittableRandom(SEED);
return Stream.generate(() -> lines(random)).limit(CASES);
}
static Stream<OrderNumber> orderNumbers() {
var random = new SplittableRandom(SEED);
return Stream.generate(() -> new OrderNumber(random.nextInt(2000, 2100), random.nextLong(1, 10_000_000)))
.limit(CASES);
}
// Surefire names a failing case by its index only, so each assertion carries its input with as(...). There is no
// shrinking: if the failing input is large, cut it down by hand to the smallest one that still fails.
@ParameterizedTest
@MethodSource("orders")
void totalIsTheSumOfItsLines(List<Line> lines) {
Order order = Order.open();
BigDecimal expected = BigDecimal.ZERO;
for (Line line : lines) {
order.addLine(line.product(), line.quantity(), line.unitPrice());
expected = expected.add(line.unitPrice().multiply(BigDecimal.valueOf(line.quantity())));
assertThat(order.total()).as("total after %s", line).isEqualByComparingTo(expected);
assertThat(order.total()).as("total after %s", line).isNotNegative();
}
}
@ParameterizedTest
@MethodSource("orderNumbers")
void anOrderNumberSurvivesPrintingAndParsing(OrderNumber number) {
assertThat(OrderNumber.parse(number.toString())).as("parsing %s", number).isEqualTo(number);
}
@ParameterizedTest
@MethodSource("orders")
void cancellingTwiceIsTheSameAsCancellingOnce(List<Line> lines) {
Order order = Order.open();
for (Line line : lines) {
order.addLine(line.product(), line.quantity(), line.unitPrice());
}
order.cancel();
OrderStatus status = order.status();
BigDecimal total = order.total();
order.cancel();
assertThat(order.status()).as("status after cancelling twice, lines %s", lines).isEqualTo(status);
assertThat(order.total()).as("total after cancelling twice, lines %s", lines).isEqualByComparingTo(total);
}
}
// The expected value comes from the rule, computed the plain way (sum of quantity x price), never by calling the
// code under test. Before calling these done, run PIT on Order and OrderNumber: a mutant that survives every property
// is a rule nobody stated. PIT setup: code-reviewer/references/testing/boot4-test-toolkit.md.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |