Testing: web app only
This commit is contained in:
		
							parent
							
								
									83e778d739
								
							
						
					
					
						commit
						8e7f231d1f
					
				
							
								
								
									
										39
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								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.
 | 
			
		||||
							
								
								
									
										30
									
								
								src/test/java/djmil/hellomvc/WebApplicationTest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/test/java/djmil/hellomvc/WebApplicationTest.java
									
									
									
									
									
										Normal file
									
								
							@ -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")));
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user