File tree 1 file changed +43
-0
lines changed
src/test/java/com/example/demo
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .example .demo ;
2
+
3
+ import static org .hamcrest .Matchers .*;
4
+ import static org .mockito .Mockito .*;
5
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
6
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
7
+
8
+ import org .junit .Test ;
9
+ import org .junit .runner .RunWith ;
10
+ import org .springframework .beans .factory .annotation .Autowired ;
11
+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
12
+ import org .springframework .boot .test .context .SpringBootTest ;
13
+ import org .springframework .boot .test .mock .mockito .MockBean ;
14
+ import org .springframework .security .test .context .support .WithMockUser ;
15
+ import org .springframework .test .context .junit4 .SpringRunner ;
16
+ import org .springframework .test .web .servlet .MockMvc ;
17
+
18
+ import com .example .demo .domain .service .UserService ;
19
+
20
+ @ RunWith (SpringRunner .class )
21
+ @ SpringBootTest
22
+ @ AutoConfigureMockMvc
23
+ public class HomeControllerTest {
24
+
25
+ @ Autowired
26
+ private MockMvc mockMvc ;
27
+
28
+ @ MockBean
29
+ private UserService service ;
30
+
31
+ @ Test
32
+ @ WithMockUser
33
+ public void ユーザーリスト画面のユーザー件数のテスト () throws Exception {
34
+
35
+ // UserServiceのcountメソッドの戻り値を10に設定
36
+ when (service .count ()).thenReturn (10 );
37
+
38
+ // ユーザーリスト画面のチェック
39
+ mockMvc .perform (get ("/userList" ))
40
+ .andExpect (status ().isOk ())
41
+ .andExpect (content ().string (containsString ("合計:10件" )));
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments