【发布时间】:2017-08-25 06:16:32
【问题描述】:
我想测试一下,我无权这样做。 这是我的代码:
/* imports */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class AuthenticationTest {
private UsernamePasswordAuthenticationToken authentication;
@Autowired
private AuthenticationManager authManager;
public void before() throws Exception {
this.authentication = new UsernamePasswordAuthenticationToken("username", "password");
SecurityContextHolder.getContext().setAuthentication(manager.authenticate(authentication));
}
@Test(expected = AccessDeniedException.class)
public void postExperience() throws Exception {
ExperienceEntity experience = new ExperienceEntity();
experience.setExperience("Test");
experience.setExperienceEng("Test");
mockMvc.perform(
post(URL_EXPERIENCES).principal(authentication).content(json(experience)).contentType(CONTENT_TYPE))
.andExpect(status().isForbidden());
}
错误日志:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.security.access.AccessDeniedException: Access is denied
我不明白为什么这个测试不起作用。我收到了这些错误,这是我预期的。
【问题讨论】:
标签: java spring junit spring-security