title: Setting up MockMvc with @WebMvcTest in Spring Boot 1.4 MVC Testing
body: <p>I have few working code to set up <code>MockMVc</code> in different ways with the new Spring Boot 1.4 <code>@WebMvcTest</code>. I understand the standaloneSetup approach. What I want to know is the difference between setting up <code>MockMvc</code> through <code>WebApplicationContext</code> and by autowiring <code>MockMvc</code>.</p>
  
  <p><strong>Code Snippet 1: MockMvc through WebApplicationContext Setup</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(controllers = ProductController.class)
  public class ProductControllerTest {
  
  private MockMvc mockMvc;
  
  @Autowired
  private WebApplicationContext webApplicationContext;
  
  @MockBean
  private ProductService productServiceMock;
  
  @Before
  public void setUp() {
       mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }
  
  @Test
  public void testShowProduct() throws Exception {      
  
      Product product1 = new Product();
      /*Code to initialize product1*/
  
      when(productServiceMock.getProductById(1)).thenReturn(product1);
  
      MvcResult result = mockMvc.perform(get("/product/{id}/", 1))
              .andExpect(status().isOk())
              /*Other expectations*/
              .andReturn();
    }
  }
  </code></pre>
  
  <p>As per <code>WebMvcTest</code> API documentation, <em>By default, tests annotated with @WebMvcTest will also auto-configure Spring Security and MockMvc</em>. So, I expected a 401 Unauthorized status code here, but the test passes with a 200 status code.</p>
  
  <p>Next, I tried auto wiring <code>MockMvc</code>, but the test fails with 401 Unauthorized status code, unless I add <code>@AutoConfigureMockMvc(secure=false)</code> or update the <code>@WebMvcTest</code> annotation to disable security:</p>
  
  <pre><code>@WebMvcTest(controllers = IndexController.class, secure = false)
  </code></pre>
  
  <p><br>
  Following is the code that passes ONLY AFTER explicitly disabling security.<br><br>
  <strong>Code Snippet 2: MockMvc through Autowiring</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(controllers = ProductController.class)
  @AutoConfigureMockMvc(secure=false)
  public class ProductControllerTest {
  @Autowired
  private MockMvc mockMvc;
  @Autowired
  private WebApplicationContext webApplicationContext;
  @MockBean
  private ProductService productServiceMock;
  
  @Test
  public void testShowProduct() throws Exception {      
  
      Product product1 = new Product();
      /*Code to initialize product1*/
  
      when(productServiceMock.getProductById(1)).thenReturn(product1);
  
      MvcResult result = mockMvc.perform(get("/product/{id}/", 1))
              .andExpect(status().isOk())
              /*Other expectations*/
              .andReturn();
    }
  }
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>Why didn't Code snippet 1 report a a 401 Unauthorized status code error while auto wiring <code>MockMvc</code> did. Also reiterating what the official doc says  <em>By default, tests annotated with @WebMvcTest will also auto-configure Spring Security and MockMvc.</em> But, in this case it appears <code>@WebMvcTest</code> has nothing to do with auto-configuring Spring Security (Because Code Snippet 1 passes without any 401 error). It finally boils down to how I set up the <code>MockMvc</code>. Am I correct here?</p></li>
  <li><p>What are the differences/objectives between/of both the approaches?</p></li>
  <li><p>How does disabling security via <code>@AutoConfigureMockMvc(secure=false)</code> differs from doing through  <code>@WebMvcTest(controllers = IndexController.class, secure = false)</code>. Which one is the preferred approached or when (or where) to use them?</p></li>
  </ol>