有没有什么方法可以用JUnit和Mockito来测试它?
下面是我想测试的一个方法
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logoutPage(HttpServletRequest request, HttpServletResponse response)
{
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null)
{
new SecurityContextLogoutHandler().logout(request, response, auth);
}
return "redirect:/login?logout";
}
注销测试
@Test
public void testLogoutPage() throws Exception
{
MockHttpServletRequestBuilder requestBuilder = get("/logout");
MockMvcBuilders.standaloneSetup(this.loginController)
.build()
.perform(requestBuilder)
.andExpect(status().isFound())
.andExpect(MockMvcResultMatchers.model().size(0))
.andExpect(view().name("redirect:/login?logout"))
.andExpect(MockMvcResultMatchers.redirectedUrl("/login?logout"));
}
测试涵盖了一切,除了。
new SecurityContextLogoutHandler().logout(request, response, auth);
我尝试了一些Assert,但总是得到NullPointerException。
1条答案
按热度按时间neskvpey1#
您可以通过向expect链添加unauthenticated()来测试您是否已注销。