Skip to content

Commit 2b5c2d8

Browse files
authored
Merge pull request #15 from kento-kotlin-sandbox/future/add_spring_test
Future/add spring test
2 parents f58dafe + d00554d commit 2b5c2d8

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@
9696
<artifactId>spring-boot-starter-test</artifactId>
9797
<scope>test</scope>
9898
</dependency>
99+
<!-- Spring Security Test -->
100+
<dependency>
101+
<groupId>org.springframework.security</groupId>
102+
<artifactId>spring-security-test</artifactId>
103+
<scope>test</scope>
104+
</dependency>
99105
</dependencies>
100106

101107
<build>
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.demo;
2+
3+
import static org.hamcrest.CoreMatchers.containsString;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
6+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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.test.context.junit4.SpringRunner;
14+
import org.springframework.test.web.servlet.MockMvc;
15+
16+
17+
// Springモック
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest
20+
@AutoConfigureMockMvc
21+
public class LoginControllerTest {
22+
@Autowired
23+
private MockMvc mockMvc;
24+
25+
@Test
26+
public void ログイン画面表示() throws Exception {
27+
mockMvc.perform(get("/login"))
28+
.andExpect(status().isOk())
29+
.andExpect(content().string(containsString("ユーザーID")));
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.demo;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.jdbc.Sql;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
import com.example.demo.domain.model.repository.UserDao;
15+
16+
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest
19+
@Transactional
20+
public class UserDaoTest {
21+
22+
@Autowired
23+
@Qualifier("UserDaoJdbcImpl")
24+
UserDao dao;
25+
26+
// カウントメソッドのテスト1
27+
@Test
28+
public void countTest1() {
29+
// カウントメソッドの結果が2件であること
30+
assertEquals(dao.count(), 2);
31+
}
32+
33+
// カウントメソッドのテスト2
34+
@Test
35+
@Sql("/testdata.sql")
36+
public void countTest2() {
37+
38+
// カウント結果が3件であること
39+
assertEquals(dao.count(), 3);
40+
}
41+
}

src/test/resources/testdata.sql

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* テスト用データ(ADMIN権限) */
2+
INSERT INTO m_user (user_id, password, user_name, birthday, age, marriage, role)
3+
VALUES('[email protected]', '$2a$10$xRTXvpMWly0oGiu65WZlm.3YL95LGVV2ASFjDhe6WF4.Qji1huIPa', 'Kento755', '1993-07-05', 25, false, 'ROLE_ADMIN');

0 commit comments

Comments
 (0)