Struts 2.5 Junit 测试设置 spring 集成

Struts 2.5 Junit test setup with spring integration

struts版本:2.5.25

spring版本:4.3.26

使用:struts2-rest-plugin,struts2-spring-plugin,JUnit 4

我正在寻求帮助来更正我的单元测试配置,该配置目前抛出异常“ServletContext 不能为空”。下面是堆栈跟踪:

Unable to instantiate Action, com.hs.iws.actions.security.LoginAction,  defined for 'login' in namespace '/security'Error creating bean with name 'com.hs.iws.actions.security.LoginAction': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: ServletContext must not be null
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:320)
    at com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:401)
    at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:201)
    at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:76)
    at org.apache.struts2.rest.RestActionProxyFactory.createActionProxy(RestActionProxyFactory.java:50)
    at org.apache.struts2.StrutsJUnit4TestCase.getActionProxy(StrutsJUnit4TestCase.java:156)
    at com.hs.iws.actions.security.LoginActionTest.testValidation(LoginActionTest.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.hs.iws.actions.security.LoginAction': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: ServletContext must not be null
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1611)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:398)
    at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:201)
    at com.opensymphony.xwork2.spring.SpringObjectFactory.buildBean(SpringObjectFactory.java:169)
    at com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:177)
    at com.opensymphony.xwork2.factory.DefaultActionFactory.buildAction(DefaultActionFactory.java:40)
    at com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:142)
    at com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:301)
    ... 43 more
Caused by: java.lang.IllegalArgumentException: ServletContext must not be null
    at org.springframework.util.Assert.notNull(Assert.java:134)
    at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:108)
    at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:98)
    at com.hs.iws.actions.IwsActionSupport.getSpringContext(IwsActionSupport.java:166)
    at com.hs.iws.actions.IwsActionSupport.setupActionSupport(IwsActionSupport.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
    ... 52 more

单元测试(spring beans 通过注释配置,我们没有使用任何 xml)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LoginActionSpringConfig.class)
public class LoginActionTest extends IwsStrutsSpringTestCase<LoginAction> {
    
    // THIS TEST RUNS OKAY
    @Test
    public void getActionMapping() {
        ActionMapping mapping = getActionMapping("/security/login");
        Assert.assertNotNull(mapping);
        Assert.assertEquals("/security", mapping.getNamespace());
        Assert.assertEquals("login", mapping.getName());
    }


    // THIS TEST PRODUCES THE ERROR WHEN CALLING getActionProxy()
    @Test
    public void testValidation() {
        
        // the login uses the create() which expects the POST method
        request.setMethod("POST");

        ActionProxy proxy = getActionProxy("/security/login");
        Assert.assertNotNull(proxy);

        LoginAction action = (LoginAction)proxy.getAction();
        Assert.assertNotNull(action);

        Assert.assertTrue("expected some field errors", !action.getFieldErrors().isEmpty());
    }
}

LoginActionSpringConfig.class

@Configuration
public class LoginActionSpringConfig {

    @Bean
    public IIwsAuthenticationService authenticationService() {
        return new IwsAuthenticationServiceTest();
    }

    @Bean
    public ISecurityLogDao securityLogDao() {
        return new SecurityLogDaoTest();
    }

    @Bean
    public IUsersDepartmentDao usersDepartmentDao() {
        return new UsersDepartmentDaoTest();
    }

    @Bean
    public IDepartmentDao departmentDao() {
        return new DepartmentDaoTest();
    }

    @Bean
    public IwsCookieInterface iwsCookie() {
        return new IwsCookieTest();
    }

}

我的 Web 应用程序当前使用 org.springframework.web.context.ContextLoaderListener 在 web.xml

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

然后指定这个contextClass

<context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>

我不确定,但我怀疑我需要在我的单元测试配置中做一些额外的事情来解决这个问题。到目前为止,我一直找不到答案。

StrutsJUnit4TestCase 中有一个 servletContext 字段,我已经确认它不是空的。但是,我怀疑这只是为了满足 struts 而不是 spring。在通过 Junit 4 测试我的 Struts 操作时,有人可以帮助我指出正确的方向来配置这个丢失的 ServletContext 吗?

11 月 1 日更新 下面是调试器的屏幕截图。我想说的是我相信有两个 ServletContext,一个在 struts 中,一个在 spring 中。看起来 struts 测试已经在设置中填充了 servletContext。我的配置中是否缺少某些东西来满足 spring 的空 ServletContext?

11 月 1 日更新 我已经弄清楚错误的来源。正在测试的 LoginAction 继承了这个在 super class.

中调用的方法
protected WebApplicationContext getSpringContext() {
        return WebApplicationContextUtils
                .getWebApplicationContext(ServletActionContext.getServletContext());
    }

ServletActionContext.getServletContext() 返回空值,这是问题的原因。我如何在单元测试中连接 ServletActionContext 以按预期运行?

我能够通过覆盖 StrutsJunit4TestCase.getActionProxy() 来解决问题。我当前的操作设置试图在构建操作时访问 WebApplicationContext,这发生在当前 getActionProxy() 将必要的对象注入 ServletActionContext 之前。下面是我解决这个问题的覆盖

    @Override
protected ActionProxy getActionProxy(String uri) {

    // the super.getActionProxy() already sets this, but does it at the end of the method.
    // the ActionProxy proxy = ... line is already expecting it to be present and produces a NPE
    ServletActionContext.setServletContext(servletContext);
    ServletActionContext.setRequest(request);
    ServletActionContext.setResponse(response);

    return super.getActionProxy(uri);
}