spring 3.1

 

 
  
  1. @RunWith(SpringJUnit4Cla***unner.class)   
  2. @ContextConfiguration(locations = {"classpath:root-context.xml",    
  3.         "classpath:servlet-context.xml""classpath:security-app-context.xml"})   
  4. public class TestSecureCtrl extends AbstractTransactionalJUnit4SpringContextTests {   
  5.     @Autowired   
  6.     private SecureCtrl controller;   
  7.     @Autowired   
  8.     private RequestMappingHandlerAdapter handlerAdapter;   
  9.    
  10.     private final MockHttpServletRequest request = new MockHttpServletRequest();   
  11.     private final MockHttpServletResponse response = new MockHttpServletResponse();   
  12.        
  13.     @Test   
  14.     public void testMain4User() throws Exception {   
  15.         request.setRequestURI("/secure");   
  16.         request.setMethod(HttpMethod.POST.name());   
  17.         HttpSession session = request.getSession();   
  18.            
  19.         //设置springsecure的内容   
  20.         List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();   
  21.         ga.add(new GrantedAuthorityImpl("ROLE_ALL"));   
  22.         User user = new User("account""password"truetruetruetrue, ga);   
  23.         SecurityContextHolder.getContext().setAuthentication(   
  24.         new UsernamePasswordAuthenticationToken(user, null));   
  25.         session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());   
  26.    
  27.         ModelAndView mav = handlerAdapter.handle(request, response, new HandlerMethod(controller, "home", Model.class, HttpServletRequest.class));   
  28.    
  29.         assertEquals("home", mav.getViewName());   
  30.     }   
  31. }   

 

spring 3.0

 

 
  
  1. @RunWith(SpringJUnit4Cla***unner.class)   
  2. @ContextConfiguration(locations = {"classpath:app-config.xml""classpath:mvc-config.xml"})   
  3. public class TestMainCtrl extends AbstractTransactionalJUnit4SpringContextTests {   
  4.     @Autowired   
  5.     private MainCtrl controller;   
  6.    
  7.     //这种方法适用于Springframework3.0,3.1换了handler的处理类。   
  8.     @Autowired   
  9.     private AnnotationMethodHandlerAdapter handlerAdapter;   
  10.    
  11.     private final MockHttpServletRequest request = new MockHttpServletRequest();   
  12.     private final MockHttpServletResponse response = new MockHttpServletResponse();   
  13.    
  14.     @Test   
  15.     public void testMain4User() throws Exception {   
  16.         request.setRequestURI("/main");   
  17.         request.setMethod(HttpMethod.POST.name());   
  18.         HttpSession session = request.getSession();   
  19.         //设置 认证信息   
  20.         session.setAttribute(CommonConstants.SESSION_USER_TYPE, 1);   
  21.         session.setAttribute(CommonConstants.SESSION_USER_ID, 0);   
  22.         session.setAttribute(CommonConstants.SESSION_USER_ACC, "aa1");   
  23.    
  24.         ModelAndView mav = handlerAdapter.handle(request, response, controller);   
  25.         assertEquals("/regist", mav.getViewName());   
  26.     }   
  27. }