2011-09-07 163 views
2

我必須構建一個單元測試來測試一些用戶操作,當它們通過身份驗證時。如何模擬Spring SecurityContext,以便我可以將它與TestNG一起使用?

我擁有EasyMock和TestNG的所有功能。

但我不能找到一種方法來注入SecurityContextHolderStrategy(我使用這個接口,爲了能夠注入和模擬SecurityContextHolder的在我的控制器,所以我可以有2只不同的設置一個用於生產,一個用於測試使用分離的applicationContext .xml)

但是我很難創建一個可以使SecurityContextHolderStrategy與正確設置相匹配的bean(在測試一個空的上下文並在prod中注入真正的設置)。

任何人都可以幫助我嗎?

這裏是控制器的代碼示例。

@Controller 
@RequestMapping("/topic/**") 
public class TopicController { 

@Autowired 
PostRepository postRepo; 
@Autowired 
TopicRepository top; 
@Autowired 
PersonRepository per; 
@Autowired 
ProductRepository pro; 
@Autowired 
TopicControllerHelper topHelper; 
@Autowired 
SecurityContextHolderStrategy securityContext; 

@RequestMapping(value="/topic/{topicId}", method=RequestMethod.GET) 
public ModelAndView showPage(@PathVariable("topicId") int id){ 
    ModelAndView model = new ModelAndView("/topic"); 
    Topic topic = top.findTopicByID((long) id); 
    model.addObject("topic",topic); 
    Post post = new Post(); 
    post.setPerson(topic.getPerson()); 
    post.setTopic(topic); 
    model.addObject("post",post); 
    model.addObject("logged",securityContext.getContext().getAuthentication().getName()); 
    return model; 
} 

而且我testApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 

    <context:spring-configured /> 
    <context:component-scan base-package="br.com.gsc" /> 
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/> 
<!-- <tx:annotation-driven transaction-manager="transactionManager"/>  --> 
    <mvc:annotation-driven /> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="gscTest" /> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <bean id="securityContext" class="org.springframework.security.core.context.SecurityContextHolderStrategy"> 

我迷失在這裏!在測試和生產 中應該在哪裏使上下文在每個.xml中工作?

</bean> 

    <!-- <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" 
     p:definitions="/WEB-INF/tiles-defs.xml" /> --> 

</beans> 

回答

0

我知道這是不是一個直接回答你的問題,但你有沒有想過使用Powermock嘲笑靜態SecurityContextHolder.getSecurityContext()方法?

0

創建一個單獨的bean,其中包含可以模擬的接口,並讓該bean從靜態上下文中讀取Authentication。如果你有一個用戶對象,它甚至可以返回一個用戶而不是一個名字。

相關問題