Skip to content

Commit 4be5520

Browse files
committed
测试用例 请求正则
1 parent 5a5333e commit 4be5520

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

security/rest-scu/src/main/java/com/huifer/restsec/controller/UserController.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.huifer.restsec.controller;
22

3+
import com.fasterxml.jackson.annotation.JsonView;
34
import com.huifer.restsec.entity.dto.UserInfo;
45
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
57
import org.springframework.web.bind.annotation.RestController;
68

79
import java.util.ArrayList;
@@ -11,16 +13,28 @@
1113
@RestController
1214
public class UserController {
1315
@GetMapping("/hello")
16+
@JsonView(UserInfo.UserSimpleView.class)
1417
public List<UserInfo> hello() {
1518
List<UserInfo> userInfos = new ArrayList<>();
1619

1720
for (int i = 0; i < 10; i++) {
1821
UserInfo userInfo = new UserInfo();
1922
userInfo.setName(UUID.randomUUID().toString());
23+
userInfo.setPwd(UUID.randomUUID().toString());
2024
userInfos.add(
2125
userInfo
2226
);
2327
}
2428
return userInfos;
2529
}
30+
31+
@JsonView(UserInfo.UserDetailView.class)
32+
@GetMapping("/user/{id:\\d+}")
33+
public UserInfo findById(@PathVariable(value = "id") String id) {
34+
UserInfo userInfo = new UserInfo();
35+
userInfo.setName(String.valueOf(id));
36+
userInfo.setPwd(String.valueOf(id));
37+
38+
return userInfo;
39+
}
2640
}

security/rest-scu/src/main/java/com/huifer/restsec/entity/dto/UserInfo.java

+19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
package com.huifer.restsec.entity.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonView;
4+
35
public class UserInfo {
46

7+
public interface UserSimpleView{}
8+
public interface UserDetailView extends UserSimpleView{}
9+
10+
11+
@JsonView(UserSimpleView.class)
512
private String name;
613

14+
15+
@JsonView(UserDetailView.class)
16+
private String pwd;
17+
18+
public String getPwd() {
19+
return pwd;
20+
}
21+
22+
public void setPwd(String pwd) {
23+
this.pwd = pwd;
24+
}
25+
726
public String getName() {
827

928
return name;

security/rest-scu/src/test/java/com/huifer/restsec/RestSecApplicationTests.java

+32-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import org.springframework.http.MediaType;
99
import org.springframework.test.context.junit4.SpringRunner;
1010
import org.springframework.test.web.servlet.MockMvc;
11+
import org.springframework.test.web.servlet.ResultActions;
1112
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
13+
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
1214
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
1315
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1416
import org.springframework.web.context.WebApplicationContext;
@@ -34,7 +36,36 @@ public void contextLoads() throws Exception {
3436
.contentType(MediaType.APPLICATION_JSON_UTF8)
3537

3638
).andExpect(MockMvcResultMatchers.status().isOk())
37-
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(10));
39+
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(10))
40+
.andDo(MockMvcResultHandlers.print());
41+
42+
}
43+
44+
45+
@Test
46+
public void findById() throws Exception {
47+
mockMvc.perform(
48+
MockMvcRequestBuilders.get("/user/123")
49+
.contentType(MediaType.APPLICATION_JSON_UTF8)
50+
51+
).andExpect(MockMvcResultMatchers.status().isOk())
52+
.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("123"))
53+
.andDo(MockMvcResultHandlers.print());
54+
55+
3856
}
3957

58+
59+
@Test
60+
public void findByIdError() throws Exception {
61+
ResultActions resultActions = mockMvc.perform(
62+
MockMvcRequestBuilders.get("/user/df")
63+
.contentType(MediaType.APPLICATION_JSON_UTF8)
64+
65+
).andExpect(MockMvcResultMatchers.status().is4xxClientError())
66+
.andDo(MockMvcResultHandlers.print());
67+
68+
69+
System.out.println();
70+
}
4071
}

0 commit comments

Comments
 (0)