1
我想測試,我沒有被授權這樣做。 這裏我的代碼:JUnit測試與彈簧安全
/* 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
我不明白爲什麼這個測試不工作。我收到了這些我期望的錯誤。
謝謝。有用! – Paddy3108