JUnit
Spring Security
Testing
@WithMockUser
Import Error

Why cant i import WithMockUser to my test

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

If @WithMockUser cannot be imported in a Spring test, the cause is usually simple: the Spring Security test module is missing from the test classpath. In some cases the dependency is present, but version mismatches or IDE cache issues still prevent resolution. The fastest path is to verify the dependency first, then confirm package import and test configuration.

Where @WithMockUser Comes From

@WithMockUser is not part of core Spring Security runtime. It lives in the Spring Security test support module.

The import is:

java
import org.springframework.security.test.context.support.WithMockUser;

If that package is unavailable, the test dependency is missing or not being resolved correctly.

Add the Correct Dependency

For Maven:

xml
1<dependency>
2    <groupId>org.springframework.security</groupId>
3    <artifactId>spring-security-test</artifactId>
4    <scope>test</scope>
5</dependency>

For Gradle:

gradle
testImplementation "org.springframework.security:spring-security-test"

If you already use Spring Boot starter test dependencies, this module is still separate and must be added explicitly when you want security test helpers.

Check Version Alignment

The test module version should align with the Spring Security version used by the application. If you use Spring Boot dependency management, let Boot manage versions instead of hardcoding a mismatched one.

If versions drift, IDE imports can fail or tests can compile but behave strangely at runtime.

Minimal Example That Uses @WithMockUser

Once the dependency is available, a basic MVC test looks like this:

java
1import org.junit.jupiter.api.Test;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
4import org.springframework.security.test.context.support.WithMockUser;
5import org.springframework.test.web.servlet.MockMvc;
6
7import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
9
10@WebMvcTest
11class SecuredControllerTest {
12
13    @Autowired
14    private MockMvc mockMvc;
15
16    @Test
17    @WithMockUser(username = "alice", roles = "USER")
18    void securedEndpointIsAccessibleToMockUser() throws Exception {
19        mockMvc.perform(get("/profile"))
20            .andExpect(status().isOk());
21    }
22}

That confirms both import resolution and actual Spring Security test support are working.

IDE Problems After Adding the Dependency

Sometimes the dependency is correct but the IDE still cannot import it immediately.

Typical fixes:

  • Reimport Maven or Gradle project.
  • Invalidate IDE caches if resolution is stale.
  • Run a command-line build to confirm the classpath is correct outside the IDE.

For Gradle:

bash
./gradlew testClasses

For Maven:

bash
mvn test-compile

If the command-line build works, the problem is usually IDE state rather than project configuration.

Test Slice and Security Context Caveats

Importing @WithMockUser is only step one. The annotation is useful when Spring Security test infrastructure is actually participating in the test.

For MVC tests, that usually means using Spring test support with the security filter chain active. For plain unit tests with no Spring test context, the annotation alone does nothing useful.

So the two questions are separate:

  • Can the annotation be imported.
  • Is the test configured so that it matters.

Alternative for Custom Authentication Needs

If @WithMockUser is too simple for your use case, Spring Security test also provides helpers such as request post-processors or custom security context factories. But import issues should be solved first by getting the right dependency onto the test classpath.

Do not overcomplicate the setup before the basic module resolution is correct.

Common Pitfalls

  • Adding Spring Security runtime dependencies but forgetting spring-security-test.
  • Using the wrong import package.
  • Forcing an incompatible version instead of relying on Spring Boot dependency management.
  • Assuming the annotation should work in tests that do not use Spring test support.
  • Treating an IDE cache problem as a dependency problem without checking command-line build output.

Summary

  • '@WithMockUser comes from spring-security-test, not from the core runtime module.'
  • Add the test dependency and keep its version aligned with the rest of Spring Security.
  • Use the import org.springframework.security.test.context.support.WithMockUser.
  • Verify with a command-line test compile if the IDE still cannot resolve it.
  • Remember that importing the annotation and using it effectively are two separate concerns.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.