List of usage examples for org.springframework.security.test.context.support WithSecurityContextFactory createSecurityContext
SecurityContext createSecurityContext(A annotation);
From source file:com.mastercard.test.spring.security.SpringSecurityJUnit4ClassRunner.java
/** * Run the test corresponding to the child, which can be assumed to be an element * of the list returned by getChildren(). Ensures that relevant test events are * reported through the notifier.//from w w w.jav a2 s . c om * * The implementation wraps the inherited runChild() method to insert the mock/test * user into the SecurityContext prior to execution and removes it after execution. * * @param frameworkMethod The method representing the child test. * @param notifier The notifier for the test execution. */ @SuppressWarnings("unchecked") @Override protected void runChild(FrameworkMethod frameworkMethod, RunNotifier notifier) { if (frameworkMethod instanceof AnnotationFrameworkMethod) { AnnotationFrameworkMethod annotationFrameworkMethod = (AnnotationFrameworkMethod) frameworkMethod; Annotation userAnnotation = annotationFrameworkMethod.getAnnotation(); WithSecurityContext withSecurityContext = userAnnotation.annotationType() .getAnnotation(WithSecurityContext.class); Class<? extends WithSecurityContextFactory<? extends Annotation>> clazz = withSecurityContext.factory(); WithSecurityContextFactory withSecurityContextFactory = buildWithSecurityContextFactory(clazz); SecurityContext securityContext = null; if (withSecurityContextFactory != null) { securityContext = withSecurityContextFactory.createSecurityContext(userAnnotation); } if (securityContext == null) { securityContext = SecurityContextHolder.createEmptyContext(); } SecurityContextHolder.setContext(securityContext); } super.runChild(frameworkMethod, notifier); SecurityContextHolder.clearContext(); }