File tree Expand file tree Collapse file tree 2 files changed +37
-3
lines changed
main/java/com/didispace/chapter11
test/java/com/didispace/chapter11 Expand file tree Collapse file tree 2 files changed +37
-3
lines changed Original file line number Diff line number Diff line change
1
+ package com .didispace .chapter11 ;
2
+
3
+ import org .springframework .web .bind .annotation .RequestMapping ;
4
+ import org .springframework .web .bind .annotation .RestController ;
5
+
6
+ @ RestController
7
+ public class HelloController {
8
+
9
+ @ RequestMapping ("/hello" )
10
+ public String index () {
11
+ return "Hello World" ;
12
+ }
13
+
14
+ }
Original file line number Diff line number Diff line change 1
1
package com .didispace .chapter11 ;
2
2
3
+ import org .junit .Before ;
3
4
import org .junit .Test ;
4
5
import org .junit .runner .RunWith ;
5
6
import org .springframework .boot .test .context .SpringBootTest ;
7
+ import org .springframework .http .MediaType ;
6
8
import org .springframework .test .context .junit4 .SpringRunner ;
9
+ import org .springframework .test .web .servlet .MockMvc ;
10
+ import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
11
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
12
+
13
+ import static org .hamcrest .Matchers .equalTo ;
14
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
15
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
16
+
7
17
8
18
@ RunWith (SpringRunner .class )
9
19
@ SpringBootTest
10
20
public class Chapter11ApplicationTests {
11
21
12
- @ Test
13
- public void contextLoads () {
14
- }
22
+ private MockMvc mvc ;
23
+
24
+ @ Before
25
+ public void setUp () throws Exception {
26
+ mvc = MockMvcBuilders .standaloneSetup (new HelloController ()).build ();
27
+ }
28
+
29
+ @ Test
30
+ public void getHello () throws Exception {
31
+ mvc .perform (MockMvcRequestBuilders .get ("/hello" ).accept (MediaType .APPLICATION_JSON ))
32
+ .andExpect (status ().isOk ())
33
+ .andExpect (content ().string (equalTo ("Hello World" )));
34
+ }
15
35
16
36
}
You can’t perform that action at this time.
0 commit comments