Skip to content

Commit 0462a5f

Browse files
author
James Little
committed
Use MockMvc for Spring web controller tests
This allows for easier error testing. Approach based on https://stackoverflow.com/questions/25901985/difference-between-mockmvc-and-resttemplate-in-integration-tests
1 parent 4e431b4 commit 0462a5f

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/main/java/com/bnc/sbjb/rest/Controller.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
@RequestMapping("${resource.path}")
1010
public class Controller {
1111

12+
@RequestMapping("/hello-error")
13+
@ResponseStatus(HttpStatus.OK)
14+
public String notYetWorld() {
15+
throw new IllegalStateException("Hello World");
16+
}
17+
1218
@RequestMapping("/hello")
1319
@ResponseStatus(HttpStatus.OK)
1420
public String helloWorld() {
15-
throw new IllegalStateException("Hello World");
21+
return "Hello World";
1622
}
1723
}
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
package com.bnc.sbjb.rest;
22

3-
import static org.assertj.core.api.Assertions.assertThat;
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
46

7+
import org.junit.jupiter.api.DisplayName;
58
import org.junit.jupiter.api.Test;
69
import org.junit.jupiter.api.extension.ExtendWith;
710
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.beans.factory.annotation.Value;
9-
import org.springframework.boot.test.context.SpringBootTest;
10-
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
11-
import org.springframework.boot.test.web.client.TestRestTemplate;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
1213
import org.springframework.test.context.junit.jupiter.SpringExtension;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.web.servlet.ResultMatcher;
1316

14-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
1517
@ExtendWith(SpringExtension.class)
16-
public class ControllerTest {
18+
@WebMvcTest(Controller.class)
19+
class ControllerTest {
1720

1821
@Value("${resource.path}")
1922
private String basePath;
2023

2124
@Autowired
22-
private TestRestTemplate template;
25+
private MockMvc mockMvc;
26+
27+
private ResultMatcher equals(String string) {
28+
return content().string(string);
29+
}
30+
31+
@Test
32+
@DisplayName("Spring says hello world")
33+
void testHelloWorld() throws Exception {
34+
mockMvc.perform(get(basePath + "/hello")).andExpect(equals("Hello World"));
35+
}
2336

2437
@Test
25-
void testHelloWorld() {
26-
String actual = template.getForObject(basePath + "/hello", String.class);
27-
assertThat(actual).isEqualTo("Hello World");
38+
@DisplayName("Spring handles errors gracefully")
39+
void testNotYetWorld() throws Exception {
40+
mockMvc.perform(get(basePath + "/hello-error")).andExpect(status().is5xxServerError());
2841
}
2942
}

0 commit comments

Comments
 (0)