[英]NullPointerException when Spring SecurityContextHolder is called
I have been trying to write a test case for the following line of code but I keep getting java.lang.NullPointerException, I have tried to follow/replicate what others have suggested here Unit testing with Spring Security but I have had no luck. 我一直在尝试为下面的代码行编写一个测试用例,但是我一直在获取java.lang.NullPointerException,我一直在尝试遵循/复制其他人在这里提出的使用Spring Security进行单元测试的建议,但是我没有运气。 Can someone please help me better identify or give me a hint what I need to do.
有人可以帮助我更好地识别或给我提示我需要做什么。 (I'm using mockito for this)
(我为此使用了Mockito)
Code: 码:
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(user)) {
continue;
}
Test case: 测试用例:
@Test
public void testExpireAllSession() throws Exception {
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Mockito.when(securityContext.getAuthentication().getPrincipal().equals(any(Object.class))).thenReturn(false);
SecurityContextHolder.setContext(securityContext);
controller.theMEthodUnderTest();
}
.. ..
There are 2 problems with your test : 您的测试有2个问题:
You must mock each "level" of method calls, you should mock : 您必须模拟方法调用的每个“级别”,还应该模拟:
But , you can't mock .equals(), see Mockito FAQ - limitations and Mockito - Issue 61 . 但是 ,您不能模拟.equals(),请参阅Mockito FAQ-限制和Mockito-Issue 61 。
You have to design your code/test differently. 您必须以不同的方式设计代码/测试。 For example, pass a 'user' principal to your method arguments, and make Authentication.getPrincipal() return another one (they will be different, thus making the equals return false) :
例如,将“用户”主体传递给您的方法参数,并使Authentication.getPrincipal()返回另一个(它们将有所不同,从而使equals返回false):
Code 码
public void theMethod(Principal user) {
...
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(user)) {
continue;
}
...
}
Test 测试
@Test public void testController() {
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
Authentication authentication = Mockito.mock(Authentication.class);
Principal principal1 = Mockito.mock(Principal.class);
Principal principal2 = Mockito.mock(Principal.class);
Mockito.when(authentication.getPrincipal()).thenReturn(principal1);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
SecurityContextHolder.setContext(securityContext);
new Controller().theMethod(principal2);
}
Few important things I did to make this work and hope this helps others as well. 我为完成这项工作所做的一些重要工作,希望对其他人也有所帮助。
@InjectMocks private static YourMainController controller;
@Mock SecurityContext securityContextMocked; @Mock Authentication authenticationMocked; @Mock Principal principal1;
@Test public void testExpireAllSession() throws Exception { List mySessions = new ArrayList<>(); Object principal="";
Date aDate = new Date();
SessionInformation sessionInformation = new SessionInformation(principal,”100000”,aDate); mySessions.add(sessionInformation); allUsers.add("Mike"); when(authenticationMocked.getPrincipal()).thenReturn(principal1); when(securityContextMocked.getAuthentication()).thenReturn(authenticationMocked); SecurityContextHolder.setContext(securityContextMocked); when(sessionRegistryMocked.getAllSessions(allUsers,false)).thenReturn(sessions); when(sessionRegistryMocked.getAllPrincipals()).thenReturn(allUsers); controller.expireAllSession(); verify(sessionRegistryMocked).getAllPrincipals();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.