java - 如何模拟 Spring SecurityContext 以便我可以将它与 TestNG 一起使用?

标签 java spring mocking spring-security

我必须构建一个单元测试来测试一些用户操作,当他们通过身份验证时。

我已使用 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/

相关文章:

java - 映射任务中的 ArrayIndexOutOfBound 异常

java - 为什么这些打印语句会有所不同?

java - 使用一个类从头开始创建 jar

java - 如何添加 jar 文件并使用命令提示符进行编译? (错误: A JNI error has occured,请检查您的安装)

java - 如何以编程方式加载 Spring webflow 流并获取其内容

java - Java中有类似RhinoMocks的东西吗?

java - 我的 spring boot 应用程序的新版本无法连接到 mysql

java - 是否可以使用 hibernate 实体类作为 GSON 的 POJO?

java - Mockito 中用于 jdbcTemplate 查询的 ClassCastException

python - 如何修补对象中的属性