Skip to content

Commit d00554d

Browse files
committed
Add userList view test
1 parent 3a448c1 commit d00554d

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)