Course topics

By WebNest Studio Engineering Team

Spring Boot Tutorial

Testing Secured Endpoints

Security configuration is code, and like all code it breaks — usually when someone adds a new endpoint or reorders a rule. The only reliable way to know that /api/admin/** really rejects ordinary users is an automated test that tries it.

Spring Security's test support lets you run requests as any user, with any roles, or with any JWT, without a real login flow. This lesson covers @WithMockUser, MockMvc request post-processors for users, JWTs, OAuth2 logins and CSRF tokens, and testing method security directly on services.

Test Dependencies in Spring Boot 4

Spring Boot 4 splits test support per technology. For a web API with security, add spring-boot-starter-webmvc-test and spring-security-test with test scope. @WebMvcTest loads only the web layer — controllers, advice, filters and your SecurityFilterChain — so security tests stay fast. Remember to @Import your security configuration class if it is not picked up automatically, and replace service dependencies with @MockitoBean (which replaced the removed @MockBean).

Choosing How to Represent the User

Pick the tool that matches how your application authenticates.

  • @WithMockUser(roles = "ADMIN") — annotation on the test method; creates a simple authenticated user.
  • @WithUserDetails("asha") — loads a real user from your UserDetailsService bean.
  • .with(user("asha").roles("USER")) — the same as @WithMockUser but per request.
  • .with(jwt().authorities(...)) — a JwtAuthenticationToken for resource servers; lets you set claims too.
  • .with(oauth2Login()) / .with(oidcLogin()) — simulate social login.
  • .with(csrf()) — adds a valid CSRF token for POST/PUT/DELETE when CSRF is enabled.

What to Assert

For every protected area, test at least three cases: anonymous (expect 401 or a login redirect), authenticated without permission (expect 403), and authenticated with permission (expect success). Add tests for ownership rules ("user A cannot read user B's order") because those are the checks most often forgotten.

Testing Method Security Without HTTP

@PreAuthorize rules on services can be tested with @SpringBootTest (or a small slice that includes the service and @EnableMethodSecurity) and @WithMockUser. Call the service method directly and assert that it either returns or throws AccessDeniedException.

Examples

Test dependencies (pom.xml)

Java
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webmvc-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
</dependency>
Output
(Versions are managed by the Spring Boot parent; no version tags needed.)

Web-layer security tests with @WithMockUser and request post-processors

Java
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(AdminController.class)
@Import(SecurityConfig.class)
class AdminControllerSecurityTest {

    @Autowired MockMvc mvc;
    @MockitoBean ReportService reportService;

    @Test
    void anonymousUsersAreRejected() throws Exception {
        mvc.perform(get("/api/admin/reports")).andExpect(status().isUnauthorized());
    }

    @Test
    @WithMockUser(roles = "USER")
    void ordinaryUsersAreForbidden() throws Exception {
        mvc.perform(get("/api/admin/reports")).andExpect(status().isForbidden());
    }

    @Test
    @WithMockUser(roles = "ADMIN")
    void adminsCanReadReports() throws Exception {
        when(reportService.summary()).thenReturn(new Summary(42));
        mvc.perform(get("/api/admin/reports"))
           .andExpect(status().isOk())
           .andExpect(jsonPath("$.orders").value(42));
    }

    @Test
    void postWithoutCsrfTokenIsRejected() throws Exception {
        mvc.perform(post("/admin/settings").with(user("admin").roles("ADMIN")))
           .andExpect(status().isForbidden());
        mvc.perform(post("/admin/settings").with(user("admin").roles("ADMIN")).with(csrf()))
           .andExpect(status().is3xxRedirection());
    }
}
Output
AdminControllerSecurityTest
  ✔ anonymousUsersAreRejected
  ✔ ordinaryUsersAreForbidden
  ✔ adminsCanReadReports
  ✔ postWithoutCsrfTokenIsRejected
Tests run: 4, Failures: 0

Testing a JWT resource server with claims and scopes

Java
@WebMvcTest(OrderController.class)
@Import(ResourceServerConfig.class)   // the SecurityFilterChain only, without the key-loading beans
class OrderControllerJwtTest {

    @Autowired MockMvc mvc;
    @MockitoBean JwtDecoder jwtDecoder;   // jwt() bypasses decoding, so no real keys are needed

    @Test
    void userScopeCanListOwnOrders() throws Exception {
        mvc.perform(get("/api/orders")
                .with(jwt().jwt(j -> j.subject("asha"))
                           .authorities(new SimpleGrantedAuthority("SCOPE_user"))))
           .andExpect(status().isOk())
           .andExpect(jsonPath("$[0]").value("Order #1001 for asha"));
    }

    @Test
    void userScopeCannotDelete() throws Exception {
        mvc.perform(delete("/api/orders/5")
                .with(jwt().authorities(new SimpleGrantedAuthority("SCOPE_user"))))
           .andExpect(status().isForbidden());
    }
}
Output
OrderControllerJwtTest
  ✔ userScopeCanListOwnOrders
  ✔ userScopeCannotDelete

Testing @PreAuthorize ownership rules on a service

Java
@SpringBootTest
class OrderServiceSecurityTest {

    @Autowired OrderService orderService;

    @Test
    @WithMockUser(username = "asha@webnest.in")
    void userCanReadOwnOrders() {
        assertThatNoException().isThrownBy(() -> orderService.findForCustomer("asha@webnest.in"));
    }

    @Test
    @WithMockUser(username = "asha@webnest.in")
    void userCannotReadSomeoneElsesOrders() {
        assertThatThrownBy(() -> orderService.findForCustomer("ravi@webnest.in"))
            .isInstanceOf(AccessDeniedException.class);
    }

    @Test
    @WithMockUser(username = "admin", roles = "ADMIN")
    void adminCanReadAnyOrders() {
        assertThatNoException().isThrownBy(() -> orderService.findForCustomer("ravi@webnest.in"));
    }
}
Output
OrderServiceSecurityTest
  ✔ userCanReadOwnOrders
  ✔ userCannotReadSomeoneElsesOrders
  ✔ adminCanReadAnyOrders

Common Mistakes

  • Testing only the "happy path" as an admin and never asserting that ordinary or anonymous users are rejected.
  • Forgetting .with(csrf()) on POST tests and concluding that authorization is broken when the 403 actually comes from the CSRF filter.
  • Using @WebMvcTest without importing the real SecurityConfig, so tests run against Spring Boot's default security instead of yours.
  • Still using @MockBean, which was removed in Spring Boot 4 — use @MockitoBean.
  • Using @WithMockUser(roles = "ROLE_ADMIN"); the roles attribute adds the prefix itself, so this produces ROLE_ROLE_ADMIN.

Exercise

Using the lesson's Test dependencies (pom.xml) example, create a small implementation or design sketch that demonstrates Add spring-security-test and the per-technology test starter such as spring-boot-starter-webmvc-test.. Add a test case or scenario that would catch Testing only the "happy path" as an admin and never asserting that ordinary or anonymous users are rejected., and describe the expected result.

Show hints
  • Start with Test Dependencies in Spring Boot 4 and compare your approach with Test dependencies (pom.xml). Identify the decision or behavior the lesson recommends.
  • Walk through your scenario step by step. Confirm that it follows Add spring-security-test and the per-technology test starter such as spring-boot-starter-webmvc-test. and that it would catch the failure described by Testing only the "happy path" as an admin and never asserting that ordinary or anonymous users are rejected..

Quick quiz

1. Which choice is a mistake to avoid in Testing Secured Endpoints?

  • A. Add spring-security-test and the per-technology test starter such as spring-boot-starter-webmvc-test.
  • B. Testing only the "happy path" as an admin and never asserting that ordinary or anonymous users are rejected.
  • C. Forgetting .with(csrf()) on POST tests and concluding that authorization is broken when the 403 actually comes from the CSRF filter.
Show answer

B. Avoid this approach: Testing only the "happy path" as an admin and never asserting that ordinary or anonymous users are rejected. The lesson recommends: Add spring-security-test and the per-technology test starter such as spring-boot-starter-webmvc-test.

Interview questions

How would you apply the guidance in Test Dependencies in Spring Boot 4 to a real task, and what failure would you guard against?

Show answer

Add spring-security-test and the per-technology test starter such as spring-boot-starter-webmvc-test. Apply this in Test Dependencies in Spring Boot 4; the example Test dependencies (pom.xml) shows one way to do it. Avoid Testing only the "happy path" as an admin and never asserting that ordinary or anonymous users are rejected. and explain how your implementation or design prevents that failure.

Key Points to Remember

  • Add spring-security-test and the per-technology test starter such as spring-boot-starter-webmvc-test.
  • Use @WithMockUser, user(), jwt(), oauth2Login() and csrf() to simulate any authentication state.
  • For each protected area, test anonymous, forbidden and allowed cases.
  • Test ownership rules on services directly and assert AccessDeniedException.
  • Import your real security configuration into slice tests.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

These examples need a JDK project with their libraries on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering