栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有HATEOAS PagedResources的Spring Boot的TestRestTemplate

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有HATEOAS PagedResources的Spring Boot的TestRestTemplate

我切换到MockMvc,一切正常:

import static org.hamcrest.Matchers.hasSize;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)@ActiveProfiles("unittest")public class MyUserRepositoryIntegrationTest {    @Autowired WebApplicationContext context;    @Autowired FilterChainProxy filterChain;    MockMvc mvc;    @Before    public void setupTests() {    this.mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(filterChain).build();    @Test    public void listUsers() throws Exception {      HttpHeaders headers = new HttpHeaders();      headers.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);      headers.add(HttpHeaders.AUTHORIZATION, "Basic " + new String(base64.enpre(("user:user").getBytes())));      mvc.perform(get(USER_URL).headers(headers))          .andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))          .andExpect(status().isOk())          .andExpect(jsonPath("$.content", hasSize(NUM_USERS)));    }}

编辑:

对于那些感兴趣的人,基于WellingtonSouza解决方案的替代解决方案:

尽管jsonpath非常强大,但我还没有找到一种使用MockMvc将JSON解组为实际对象的方法。

如果查看我发布的JSON输出,您会注意到,它不是默认的Spring DataRestHAL+JSON输出。我将属性data.rest.defaultMediaType更改为“ application /
json”。这样,我也无法让特拉弗森工作。但是,当我停用它时,以下工作原理:

import static org.assertj.core.api.Assertions.assertThat;import static org.hamcrest.Matchers.hasSize;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import org.springframework.hateoas.MediaTypes;import org.springframework.hateoas.PagedResources;import org.springframework.hateoas.client.Hop;import org.springframework.hateoas.client.Traverson;import org.springframework.http.HttpHeaders;import org.springframework.security.crypto.prec.base64;@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)@ActiveProfiles("unittest")public class MyUserRepositoryIntegrationTest {  private static HttpHeaders userHeaders;  private static HttpHeaders adminHeaders;  @LocalServerPort  private int port;  @BeforeClass  public static void setupTests() {    MyUserRepositoryIntegrationTest.userHeaders = new HttpHeaders();    MyUserRepositoryIntegrationTest.userHeaders.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);    MyUserRepositoryIntegrationTest.userHeaders.add(HttpHeaders.AUTHORIZATION,        "Basic " + new String(base64.enpre(("user:user").getBytes())));    MyUserRepositoryIntegrationTest.adminHeaders = new HttpHeaders();    MyUserRepositoryIntegrationTest.adminHeaders.add(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);    MyUserRepositoryIntegrationTest.adminHeaders.add(HttpHeaders.AUTHORIZATION,        "Basic " + new String(base64.enpre(("admin:admin").getBytes())));  }  @Test  public void listUsersSorted() throws Exception {    final ParameterizedTypeReference<PagedResources<MyUser>> resourceParameterizedTypeReference = //        new ParameterizedTypeReference<PagedResources<MyUser>>() {        };    final PagedResources<MyUser> actual = new Traverson(new URI("http://localhost:" + port + "/apiv1/data"),        MediaTypes.HAL_JSON)// .follow(Hop.rel("myUsers").withParameter("sort", "username,asc"))// .withHeaders(userHeaders)// .toObject(resourceParameterizedTypeReference);    assertThat(actual.getContent()).isNotNull().isNotEmpty();    assertThat(actual.getContent()//        .stream()//        .map(user -> user.getUsername())//        .collect(Collectors.toList())//    ).isSorted();  }}

(注意:可能不包含所有导入内容,因为我是从较大的Test Class复制而来的。)

“ .withParam”适用于模板化URL,即那些接受查询参数的URL。如果您尝试使用原始URL,它将失败,因为链接的字面意思是“ http://[…] / users {option1,option2,…}”,因此格式不正确。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466204.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号