diff --git a/README.md b/README.md index 71134d9..8f1e8d9 100644 --- a/README.md +++ b/README.md @@ -204,3 +204,42 @@ public class HttpRequestTest { ``` Note the use of `webEnvironment=RANDOM_PORT` to start the server with a random port (useful to avoid conflicts in test environments) and the injection of the port with `@LocalServerPort`. Also, note that Spring Boot has automatically provided a `TestRestTemplate` for you. All you have to do is add `@Autowired` to it. + +## Test Web app only + +Another useful approach is **to not start the server at all** but to test only the layer below that, where Spring handles the incoming HTTP request and hands it off to your controller. That way, almost of the full stack is used, and your code will be called in exactly the same way as if it were processing a real HTTP request but without the cost of starting the server. To do that, use Spring’s `MockMvc` and ask for that to be injected for you by using the `@AutoConfigureMockMvc` annotation on the test case. The following listing (from `src/test/java/djmil/hellomvc/WebApplicationTest.java`) shows how to do so: + +```java +package djmil.hellomvc; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +public class WebApplicationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void shouldReturnDefaultMessage() throws Exception { + this.mockMvc.perform(get("/greeting")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Hello, World"))); + } +} +``` + +In this test, the full Spring application context is started but without the server. \ No newline at end of file diff --git a/src/test/java/djmil/hellomvc/WebApplicationTest.java b/src/test/java/djmil/hellomvc/WebApplicationTest.java new file mode 100644 index 0000000..e34b52d --- /dev/null +++ b/src/test/java/djmil/hellomvc/WebApplicationTest.java @@ -0,0 +1,30 @@ +package djmil.hellomvc; + +import static org.hamcrest.Matchers.containsString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +public class WebApplicationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void shouldReturnDefaultMessage() throws Exception { + this.mockMvc.perform(get("/greeting")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Hello, World"))); + } +}