diff --git a/README.md b/README.md index 24a069a..71134d9 100644 --- a/README.md +++ b/README.md @@ -167,4 +167,40 @@ The `@SpringBootTest` annotation tells Spring Boot to look for a main configurat Spring interprets the `@Autowired` annotation, and the controller is injected before the test methods are run. We use [AssertJ](http://joel-costigliola.github.io/assertj/) (which provides `assertThat()` and other methods) to express the test assertions. -> A nice feature of the Spring Test support is that the application context is cached between tests. That way, if you have multiple methods in a test case or multiple test cases with the same configuration, they incur the cost of starting the application only once. You can control the cache by using the @DirtiesContext annotation.| +> A nice feature of the Spring Test support is that the application context is cached between tests. That way, if you have multiple methods in a test case or multiple test cases with the same configuration, they incur the cost of starting the application only once. You can control the cache by using the @DirtiesContext annotation. + +## Test HTTP requests + +It is nice to have a sanity check, but you should also write some tests that assert the behavior of your application. To do that, you could start the application and listen for a connection (as it would do in production) and then send an HTTP request and assert the response. The following listing (from `src/test/java/djmil/hellomvc/HttpRequestTest.java`) shows how to do so: + +```java +package djmil.hellomvc; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.beans.factory.annotation.Value; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class HttpRequestTest { + + @Value(value="${local.server.port}") + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void greetingShouldReturnDefaultMessage() throws Exception { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/greeting", + String.class)).contains("Hello, World"); + } +} +``` + +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. diff --git a/src/test/java/djmil/hellomvc/HttpRequestTest.java b/src/test/java/djmil/hellomvc/HttpRequestTest.java new file mode 100644 index 0000000..6905126 --- /dev/null +++ b/src/test/java/djmil/hellomvc/HttpRequestTest.java @@ -0,0 +1,27 @@ +package djmil.hellomvc; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.beans.factory.annotation.Value; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class HttpRequestTest { + + @Value(value="${local.server.port}") + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void greetingShouldReturnDefaultMessage() throws Exception { + assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/greeting", + String.class)).contains("Hello, World"); + } +}