Difference between using MockMvc with SpringBootTest and Using WebMvcTest
<p>I am new to Spring Boot and am trying to understand how testing works in SpringBoot. I am a bit confused about what is the difference between the following two code snippets:</p>
<p>Code snippet 1:</p>

<pre class="lang-java prettyprint-override"><code>@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
public class HelloControllerApplicationTest {
    @Autowired    
    private MockMvc mvc;

    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get(&quot;/&quot;).accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo(&quot;Greetings from Spring Boot!&quot;)));
    }
}
</code></pre>
<p>This test uses the <code>@WebMvcTest</code> annotation which I believe is for feature slice testing and only tests the MVC layer of a web application.</p>
<p>Code snippet 2:</p>
<pre class="lang-java prettyprint-override"><code>@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void getHello() throws Exception {
    mvc.perform(MockMvcRequestBuilders.get(&quot;/&quot;).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(equalTo(&quot;Greetings from Spring Boot!&quot;)));
    }
}
</code></pre>
<p>This test uses the <code>@SpringBootTest</code> annotation and a <code>MockMvc</code>. So how is this different from code snippet 1? What does this do differently?</p>
<p>Edit:
Adding Code Snippet 3 (Found this as an example of integration testing in the Spring documentation)</p>
<pre class="lang-java prettyprint-override"><code>@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
public class HelloControllerIT {
    
    @LocalServerPort private int port;
    private URL base;
    
    @Autowired private TestRestTemplate template;
    
    @Before public void setUp() throws Exception {
        this.base = new URL(&quot;http://localhost:&quot; + port + &quot;/&quot;);
    }
    
    @Test public void getHello() throws Exception {
        ResponseEntity &lt; String &gt; response = template.getForEntity(base.toString(), String.class);
        assertThat(response.getBody(), equalTo(&quot;Greetings from Spring Boot!&quot;));
    }
}
</code></pre>