title: Spring MockMVC, Spring security and Mockito
body: <p>I'd like to test a <code>Spring Boot</code> Rest controller, which is secured using <code>Spring security</code>, and use mocks inside it. I have tried with Mockito, but I think any mocking tool should do the trick.</p>
  
  <p>To enable Spring security in my tests, I first did as follow:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Main.class)
  @TestPropertySource(value="classpath:application-test.properties")
  @WebAppConfiguration
  @ContextConfiguration
  public class MyTest{
  
      protected MockMvc mockMvc;
  
      @Autowired
      private WebApplicationContext wac;
  
      @Before
      public void setUp(){
          mockMvc = MockMvcBuilders
                  .webAppContextSetup(wac)
                  .apply(SecurityMockMvcConfigurers.springSecurity())
                  .build();
      }
  
      @Test
      public void doTheTest(){
          mockMvc.perform(post("/user/register")
              .with(SecurityMockMvcRequestPostProcessors.csrf())
              .content(someContent()));
      }
  }
  </code></pre>
  
  <p>Until there, it works well.</p>
  
  <p>After this step, I wished to add mocks to test my secured controller in isolation.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Main.class)
  @TestPropertySource(value="classpath:application-test.properties")
  @WebAppConfiguration
  @ContextConfiguration
  public class MyTest{
  
      protected MockMvc mockMvc;
  
      @Mock
      private Myservice serviceInjectedInController;
  
      @InjectMocks
      private MyController myController;
  
      @Autowired
      private WebApplicationContext wac;
  
      @Before
      public void setUp(){
          mockMvc = MockMvcBuilders
                  .webAppContextSetup(wac)
                  .apply(SecurityMockMvcConfigurers.springSecurity())
                  .build();
      }
  
      @Test
      public void doTheTest(){
          mockMvc.perform(post("/user/register")
              .with(SecurityMockMvcRequestPostProcessors.csrf())
              .content(someContent()));
      }
  }
  </code></pre>
  
  <p>Unfortunately, the mocked service is not injected in the controller, as there is nothing relating the MockMVC and the Mocks, so the mocks are not injected in the controller.</p>
  
  <p>So I tried changing the configuration of the MockMVC, as follows:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Main.class)
  @TestPropertySource(value="classpath:application-test.properties")
  @WebAppConfiguration
  @ContextConfiguration
  public class MyTest{
  
      protected MockMvc mockMvc;
  
      @Mock
      private Myservice serviceInjectedInController;
  
      @InjectMocks
      private MyController myController;
  
  
      @Before
      public void setUp(){
          mockMvc = MockMvcBuilders
                  .standAloneSetup(myController)
                  .apply(SecurityMockMvcConfigurers.springSecurity())
                  .build();
      }
  
      @Test
      public void doTheTest(){
          mockMvc.perform(post("/user/register")
              .with(SecurityMockMvcRequestPostProcessors.csrf())
              .content(someContent()));
      }
  }
  </code></pre>
  
  <p>But in this case, I have another issue. Spring security is complaining about the configuration:</p>
  
  <pre><code>java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
  </code></pre>
  
  <p>I have no other idea to make security and mocking. Any idea? Or should I do another way?</p>
  
  <p>Thanks.</p>