-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathChapter31ApplicationTests.java
More file actions
63 lines (49 loc) · 2.01 KB
/
Chapter31ApplicationTests.java
File metadata and controls
63 lines (49 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.didispace.chapter31;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter31ApplicationTests {
@Autowired
private UserService userSerivce;
@Before
public void setUp() {
// 准备,清空user表
userSerivce.deleteAllUsers();
}
@Test
public void test() {
// 插入5个用户
userSerivce.create("Tom", 10);
userSerivce.create("Mike", 11);
userSerivce.create("Didispace", 30);
userSerivce.create("Oscar", 21);
userSerivce.create("Linda", 17);
// 查询名为Oscar的用户,判断年龄是否匹配
List<User> userList = userSerivce.getByName("Oscar");
Assert.assertEquals(21, userList.get(0).getAge().intValue());
// 查数据库,应该有5个用户
Assert.assertEquals(5, userSerivce.getAllUsers());
// 删除两个用户
userSerivce.deleteByName("Tom");
userSerivce.deleteByName("Mike");
// 查数据库,应该有3个用户
Assert.assertEquals(3, userSerivce.getAllUsers());
}
}