我必须构建一个单元测试来测试一些用户操作,当他们通过身份验证时。
我已使用 EasyMock 和 TestNG 准备好一切。
但我找不到注入(inject) SecurityContextHolderStrategy 的方法(我使用此接口(interface)是为了能够在我的 Controller 中注入(inject)和模拟 SecurityContextHolder,因此我可以有 2 种不同的设置,一种用于生产,一种用于使用单独的 applicationContext 进行测试。 XML)
但我很难创建一个可以将 SecurityContextHolderStrategy 与适当设置匹配的 bean(在测试中是空上下文,在产品中注入(inject)真实上下文)。
谁能帮帮我?
这里是 Controller 的代码示例。
@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>
最佳答案
我知道这不是您问题的直接答案,但您是否考虑过使用 Powermock 模拟静态 SecurityContextHolder.getSecurityContext() 方法?
关于java - 如何模拟 Spring SecurityContext 以便我可以将它与 TestNG 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7337970/