Options to avoid using SecurityContextHolder in tests:
SecurityContextHolder
using some mock library - EasyMock for exampleSecurityContextHolder.get...
in your code in some service - for example in SecurityServiceImpl
with method getCurrentPrincipal
that implements SecurityService
interface and then in your tests you can simply create mock implementation of this interface that returns the desired principal without access to SecurityContextHolder
.