|
@@ -159,17 +159,17 @@ Now write a simple unit test that mocks the servlet request and response through
|
159
|
159
|
`src/test/java/hello/HelloControllerTest.java`
|
160
|
160
|
[source,java]
|
161
|
161
|
----
|
162
|
|
-include::initial/src/test/java/hello/HelloControllerTest.java[]
|
|
162
|
+include::complete/src/test/java/hello/HelloControllerTest.java[]
|
163
|
163
|
----
|
164
|
164
|
|
165
|
165
|
Note the use of the `MockServletContext` to set up an empty `WebApplicationContext` so the `HelloController` can be created in the `@Before` and passed to `MockMvcBuilders.standaloneSetup()`. An alternative would be to create the full application context using the `Application` class and `@Autowired` the `HelloController` into the test. The `MockMvc` comes from Spring Test and allows you, via a set of convenient builder classes, to send HTTP requests into the `DispatcherServlet` and make assertions about the result.
|
166
|
166
|
|
167
|
167
|
As well as mocking the HTTP request cycle we can also use Spring Boot to write a very simple full-stack integration test. For example, instead of (or as well as) the mock test above we could do this:
|
168
|
168
|
|
169
|
|
-`src/test/java/hello/HelloControllerTest.java`
|
|
169
|
+`src/test/java/hello/HelloControllerIT.java`
|
170
|
170
|
[source,java]
|
171
|
171
|
----
|
172
|
|
-include::initial/src/test/java/hello/HelloControllerIT.java[]
|
|
172
|
+include::complete/src/test/java/hello/HelloControllerIT.java[]
|
173
|
173
|
----
|
174
|
174
|
|
175
|
175
|
The embedded server is started up on a random port by virtue of the `@IntegrationTest("${server.port=0}")` and the actual port is discovered at runtime with the `@Value("${local.server.port}")`.
|