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/personas-oidc.java
View on GitHubpackage {{PACKAGE}};
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oidcLogin;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
import org.springframework.test.web.servlet.request.RequestPostProcessor;
/**
* Signs a MockMvc request in as a named user of an app that logs in with oauth2Login and keeps the login in a
* server-side session. The claims are what the identity provider puts in the ID token, and the roles come from the
* app's own GrantedAuthoritiesMapper (the one passed to userInfoEndpoint().userAuthoritiesMapper(), exposed as a
* bean, or constructed here when the app creates it inline, such as new RoleClaimsMapper()), so every
* test that uses a persona also checks the mapping. oidcLogin() alone skips the mapper: a "roles"
* claim set on it never becomes ROLE_ADMIN, and a role set with .authorities(...) passes even when the mapper is
* broken. Use with @Import(Personas.class):
*
* mvc.post().uri("/api/orders").with(personas.as("customer")).with(csrf())
*
* Verified on Spring Boot 4.1.1 / Spring Security 7.
*/
@TestComponent
class Personas {
/** The team's users and the ID-token claims their provider sends, with the provider's claim names. */
static final Map<String, Map<String, Object>> CLAIMS = Map.of(
"customer", Map.of("sub", "customer-1", "roles", List.of()),
"admin", Map.of("sub", "admin-1", "roles", List.of("ADMIN")),
"auditor", Map.of("sub", "auditor-1", "roles", List.of("AUDITOR")));
static final List<String> NAMES = List.copyOf(CLAIMS.keySet());
private final GrantedAuthoritiesMapper rolesMapper;
Personas(GrantedAuthoritiesMapper rolesMapper) {
this.rolesMapper = rolesMapper;
}
RequestPostProcessor as(String persona) {
Map<String, Object> claims = claims(persona);
OidcIdToken idToken = new OidcIdToken("token", Instant.now(), Instant.now().plusSeconds(300), claims);
return oidcLogin()
.idToken(token -> token.claims(all -> all.putAll(claims)))
.authorities(new ArrayList<GrantedAuthority>(
rolesMapper.mapAuthorities(List.of(new OidcUserAuthority(idToken)))));
}
private static Map<String, Object> claims(String persona) {
Map<String, Object> claims = CLAIMS.get(persona);
if (claims == null) {
throw new IllegalArgumentException("No persona " + persona + ". Add it to Personas.CLAIMS");
}
return claims;
}
}
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 |