我切换到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,…}”,因此格式不正确。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)