我正在测试我的spring mvc应用程序。要求是模仿 SecurityContext
,但我需要 Authentication
具有某个名称,不为空。有什么办法吗?
这是我的密码:
Authentication auth = Mockito.mock(Authentication.class);
SecurityContext secCont = Mockito.mock(SecurityContext.class);
Mockito.when(secCont.getAuthentication()).thenReturn(auth);
SecurityContextHolder.setContext(secCont);
2条答案
按热度按时间wmomyfyw1#
使用spring注解可以更轻松地完成这项工作
org.springframework.security.test.context.support.WithMockUser
```@Test
@WithMockUser(username = "viewUser", authorities = { "view" })
public void mytest(){}
1hdlvixo2#
你嘲笑了Spring
Authentication
此处的对象:你已经告诉Spring的
SecurityContextHolder
来储存这个Authentication
此处的对象:所以,如果你想嘲笑
Authentication
对象返回“some name”,然后在其上设置一些模拟期望。例如: