gpt4 book ai didi

Spring REST - 如何检索返回资源的 ID?

转载 作者:行者123 更新时间:2023-12-01 15:02:29 26 4
gpt4 key购买 nike

我有一个成功的集成测试,但我想添加到其中。

@Test
public void testAdd() throws Exception {
HttpHeaders httpHeaders = Common.createAuthenticationHeaders("stephane" + ":" + PASSWORD);

this.mockMvc.perform(
post("/admin").headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{ \"firstname\" : \"" + admin0.getFirstname() + "\", \"lastname\" : \"" + admin0.getLastname() + "\", \"email\" : \"" + admin0.getEmail() + "\", \"login\" : \"" + admin0.getLogin() + "\", \"password\" : \"" + admin0.getPassword() + "\", \"passwordSalt\" : \"" + admin0.getPasswordSalt() + "\" }")
).andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.firstname").value(admin0.getFirstname()))
.andExpect(jsonPath("$.lastname").value(admin0.getLastname()))
.andExpect(jsonPath("$.email").value(admin0.getEmail()))
.andExpect(jsonPath("$.login").value(admin0.getLogin()))
.andExpect(jsonPath("$.password").value(admin0.getPassword()))
.andExpect(jsonPath("$.passwordSalt").value(admin0.getPasswordSalt()))
.andExpect(header().string("Location", Matchers.containsString("/admin/")))
.andReturn();
}

例如,我可以对新创建的资源发送 GET 请求。

然后我可以对其执行 DELETE 请求并再次以 GET 结束。

这是一个合理的集成测试场景吗?

为此,我需要检索所创建资源的 ID。

有什么办法吗?

谢谢!

史蒂芬

最佳答案

我可以这样解决:

MvcResult 结果 = this.mockMvc.perform(...).andReturn();

andReturn() 调用用于将值返回到结果变量中。

String location = result.getResponse().getHeader("位置");

现在我可以使用以下场景进行测试:POST(创建)、GET(找到)、DELETE(确定)、GET(未找到)

这是整个测试:

HttpHeaders httpHeaders = Common.createAuthenticationHeaders("stephane" + ":" + PASSWORD);

MvcResult resultPost = this.mockMvc.perform(
post("/admin").headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content("{ \"firstname\" : \"" + admin0.getFirstname() + "\", \"lastname\" : \"" + admin0.getLastname() + "\", \"email\" : \"" + admin0.getEmail() + "\", \"login\" : \"" + admin0.getLogin() + "\", \"password\" : \"" + admin0.getPassword() + "\", \"passwordSalt\" : \"" + admin0.getPasswordSalt() + "\" }")
).andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.firstname").value(admin0.getFirstname()))
.andExpect(jsonPath("$.lastname").value(admin0.getLastname()))
.andExpect(jsonPath("$.email").value(admin0.getEmail()))
.andExpect(jsonPath("$.login").value(admin0.getLogin()))
.andExpect(jsonPath("$.password").value(admin0.getPassword()))
.andExpect(jsonPath("$.passwordSalt").value(admin0.getPasswordSalt()))
.andExpect(header().string("Location", Matchers.containsString("/admin/")))
.andReturn();

String location = resultPost.getResponse().getHeader("Location");
Pattern pattern = Pattern.compile("(\\d+)$");
Matcher matcher = pattern.matcher(location);
matcher.find();
Long id = Long.parseLong(matcher.group(), 10);

MvcResult resultGet = this.mockMvc.perform(
get("/admin/" + id)
.headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isFound())
.andReturn();
String responseContent = resultGet.getResponse().getContentAsString();

this.mockMvc.perform(
delete("/admin/" + id)
.headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());

this.mockMvc.perform(
get("/admin/" + id)
.headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isNotFound());

关于Spring REST - 如何检索返回资源的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20099639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com