vendredi 13 mars 2015

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. Can someone please help me better identify or give me a hint what I need to do. (I'm using mockito for this)


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();
}


..


Aucun commentaire:

Enregistrer un commentaire