Stand-alone testing a Spring security configuration

Many examples of testing Spring Security configurations out there focus on how to write an integration test covering your security configuration as well as your controller(s). This post examines one possible way of testing a security configuration in a (mostly) isolated fashion.

In a recent project I ran into a situation which had a relatively complex Spring Security configuration as well as some authorization logic sprinkled across it’s controllers. In order to gain more confidence in refactoring the security configuration, I wanted an isolated test telling me whether my modifications also changed the configuration’s behaviour. I came up with what’s documented in this post.

Let’s start out by looking at the Spring Security configuration we’ll be testing as part of this article:

protected void configure(HttpSecurity http) throws Exception {

Here we are setting up a form login and a logout page, and then proceed to say that there are three separate areas:

  • A “secure area”, consisting of all sub-pages of http://example.com/secure. All logged in users can see this content
  • An admin area, containing all sub-pages of http://example.com/admin. Only users with the ADMIN role can see this content
  • Everything else, which is world accessible

In order to test this configuration, I first created a dummy controller in my test code, which catches all GET requests:

public class DummyCatchAllController {
    @RequestMapping(path = "/**", method = RequestMethod.GET)
    public String catchAll() {
        return "DummyCatchallController#catchAll";

Then I proceeded to set up a test which, using this dummy controller, tests the security configuration. First the boilerplate:

public class SecurityConfigurationTest {
    private MockMvc mockMvc;
    @Autowired private WebApplicationContext webApplicationContext;

    public void setup() {
        this.mockMvc =


Instead of @SpringBootTest (loads your full application context) we’re using @WebMvcTest (only loads whatever the controller needs) here, because they are commonly faster[1] and we’re trying to isolate the security configuration as much as possible. Since WebMvcTests don’t load the entire application context though, we need to manually import the security configuration with


Next up, in the setup method, we’re initializing MockMVC, which will help us perform our test requests against the controller. We cannot use the @AutoConfigureMockMvc annotation here, because we need to make sure that springSecurity() is applied, since that’s what we want to test.

Now that we’ve got the setup finished, we can finally look at the actual tests. If you’re unfamiliar with how MockMVC works, and the examples are unclear, I recommend looking at this introduction.

First, let’s make sure that our world-accessible resources can be seen by any user:

public void asAnonymous_ICanAccessPublicResources() throws Exception {

Next, let’s see if a loggged in user can see the “secure area”. Here we’re using the @WithMockUser annotation to simulate a logged-in user who has the USER role.

@WithMockUser(username = "user", roles = {"USER"})
public void asLoggedInUser_ICanAccessSecureArea() throws Exception {

At the same time, we also want to make sure that a normal user can’t see the admin area:

@WithMockUser(username = "user", roles = {"USER"})
public void asLoggedInUser_ICantAccessAdminArea() throws Exception {

Finally, an admin should be able to see the admin area:

@WithMockUser(username = "admin", roles = {"USER", "ADMIN"})
public void asAdminUser_ICanAccessAdminArea() throws Exception {

Note that the usernames that we’re supplying to the @WithMockUser annotation in all of these examples does not actually have any meaning. I’ve only added it to make the test cases clearer.

That was it already. If you’re interested in a runnable example to play around with, you can find it here.

Feedback is always welcome and I try to incorporate it into the post where possible. For reasons of Datensparsamkeit I do not provide a commenting solution here, however. You can reach me via email (see home page) or Twitter.

  1. See https://stackoverflow.com/a/39869110/124257 for more info