Skip to content

Commit

Permalink
测试用例 请求正则
Browse files Browse the repository at this point in the history
  • Loading branch information
zeoFura committed Sep 24, 2019
1 parent 5a5333e commit 4be5520
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.huifer.restsec.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.huifer.restsec.entity.dto.UserInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
Expand All @@ -11,16 +13,28 @@
@RestController
public class UserController {
@GetMapping("/hello")
@JsonView(UserInfo.UserSimpleView.class)
public List<UserInfo> hello() {
List<UserInfo> userInfos = new ArrayList<>();

for (int i = 0; i < 10; i++) {
UserInfo userInfo = new UserInfo();
userInfo.setName(UUID.randomUUID().toString());
userInfo.setPwd(UUID.randomUUID().toString());
userInfos.add(
userInfo
);
}
return userInfos;
}

@JsonView(UserInfo.UserDetailView.class)
@GetMapping("/user/{id:\\d+}")
public UserInfo findById(@PathVariable(value = "id") String id) {
UserInfo userInfo = new UserInfo();
userInfo.setName(String.valueOf(id));
userInfo.setPwd(String.valueOf(id));

return userInfo;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
package com.huifer.restsec.entity.dto;

import com.fasterxml.jackson.annotation.JsonView;

public class UserInfo {

public interface UserSimpleView{}
public interface UserDetailView extends UserSimpleView{}


@JsonView(UserSimpleView.class)
private String name;


@JsonView(UserDetailView.class)
private String pwd;

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public String getName() {

return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
Expand All @@ -34,7 +36,36 @@ public void contextLoads() throws Exception {
.contentType(MediaType.APPLICATION_JSON_UTF8)

).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(10));
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(10))
.andDo(MockMvcResultHandlers.print());

}


@Test
public void findById() throws Exception {
mockMvc.perform(
MockMvcRequestBuilders.get("/user/123")
.contentType(MediaType.APPLICATION_JSON_UTF8)

).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("123"))
.andDo(MockMvcResultHandlers.print());


}


@Test
public void findByIdError() throws Exception {
ResultActions resultActions = mockMvc.perform(
MockMvcRequestBuilders.get("/user/df")
.contentType(MediaType.APPLICATION_JSON_UTF8)

).andExpect(MockMvcResultMatchers.status().is4xxClientError())
.andDo(MockMvcResultHandlers.print());


System.out.println();
}
}

0 comments on commit 4be5520

Please sign in to comment.