我正在尝试测试家庭控制器
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
我使用spring security作为用户名“user”并默认测试为密码,但@PreAuthorize不起作用
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@PreAuthorize("hasRole('ADMIN')")
public class HomeControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
@WithMockUser(username = "user", password = "test", roles = "ADMIN")
public void home() throws Exception {
String body = this.restTemplate.getForObject("/", String.class);
assertThat(body).isEqualTo("Hello World!");
}
}
结果
预期结果:
<"[你好世界!]">
实际结果:
<"{"timestamp":1501100448216,"status":401,"error":"Unauthorized","message":"访问此资源需要完全认证","path":"/"}]">
我错过了什么吗?