我不明白为什么这个方法不起作用。我觉得可能是有些简单的东西被我忽略了。其他没有使用 token 的测试方法都能正常运行。目前 token 没有过期,而且我可以在 Postman 中正常使用它。
@Test
public void getUser() throws Exception {
String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsd2lsbGlhbXMxNiIsInJvbGVzIjoidXNlciIsImlhdCI6MTUxNDQ0OTgzM30.WKMQ_oPPiDcc6sGtMJ1Y9hlrAAc6U3xQLuEHyAnM1FU";
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/api/users/lwilliams16")
.header("authentication", "Bearer " + token))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andDo(print())
.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}