Skip to content

a-pavithraa/springboot-skills-marketplace / plugins/springboot-architecture/skills/writing-springboot-tests

writing-springboot-tests

Verified

Use 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).

assets/personas-db.java

View on GitHub
package {{PACKAGE}};

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;

import java.util.List;
import java.util.Map;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.test.web.servlet.request.RequestPostProcessor;

/**
 * Signs a MockMvc request in as a named user of an app whose users and roles are in its own database, loaded by a
 * UserDetailsService (form login or HTTP Basic). The user is loaded by the app's own UserDetailsService, so every
 * test that uses a persona also checks how stored roles become authorities. user("ann").roles("ADMIN") skips it and
 * passes even when the service loses the roles. The users must exist: seed them before each test, for example with
 * @Sql("/sql/personas.sql") on the test class. 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 {

    /** Persona name and the username of the seeded user. */
    static final Map<String, String> USERS = Map.of(
            "customer", "customer@example.com",
            "admin", "admin@example.com",
            "auditor", "auditor@example.com");

    static final List<String> NAMES = List.copyOf(USERS.keySet());

    private final UserDetailsService users;

    Personas(UserDetailsService users) {
        this.users = users;
    }

    RequestPostProcessor as(String persona) {
        String username = USERS.get(persona);
        if (username == null) {
            throw new IllegalArgumentException("No persona " + persona + ". Add it to Personas.USERS");
        }
        return user(users.loadUserByUsername(username));
    }
}
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
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: 198kUpdated Claude CodeCodexVerifiedInstall