Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java StrutsSpringTestCase-多个上下文-如何按顺序正确实例化它们_Java_Spring_Junit_Struts2_Actioncontext - Fatal编程技术网

Java StrutsSpringTestCase-多个上下文-如何按顺序正确实例化它们

Java StrutsSpringTestCase-多个上下文-如何按顺序正确实例化它们,java,spring,junit,struts2,actioncontext,Java,Spring,Junit,Struts2,Actioncontext,我正在为使用JUnit的Struts2、Spring和Hibernate的项目编写集成测试用例 我的测试类扩展了StrutsSpringTestCase。应用程序需要登录/会话才能调用任何操作。代码如下: @Test public void testGetActionProxy() throws Exception { ActionProxy proxy; String result; ActionContext.getContext().setSession(

我正在为使用JUnit的Struts2、Spring和Hibernate的项目编写集成测试用例

我的测试类扩展了
StrutsSpringTestCase
。应用程序需要登录/会话才能调用任何操作。代码如下:

@Test
public void testGetActionProxy() throws Exception {
    
    ActionProxy proxy;
    String result;
    ActionContext.getContext().setSession(createUserSession()); // Not sure if this is needed here. But in order to get the session working, I need this.

    proxy = initAction("cInfo");
    assertNotNull(proxy);
        
    CustInfo action = (CustInfo) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
}
initAction()
方法:

private ActionProxy initAction(String actionUri) {
    ActionProxy proxy = getActionProxy(actionUri);
    
    ActionContext.setContext(proxy.getInvocation().getInvocationContext());  // I tried this line of code to get the ServletActionContext.getMapping().getName() to work. But no use.
    
    ActionContext actionContext = proxy.getInvocation().getInvocationContext();
    actionContext.setSession(createUserSession()); // This is for setting a session
    return proxy;
 }
在使用此方法之前,它将加载所有配置文件
struts.xml
jpaContext.xml
beans.xml

我的操作类
CustInfo
实现了
ServletRequestAware
,它有一个方法
getActionName
,如下所示:

 return ServletActionContext.getActionMapping().getName();
当我调用
result=proxy.execute()时会调用它。所以请求失败了

问题1:为什么返回
null
?我认为
ServletActionContext
是自动启动的,所以它应该返回一个值。但事实并非如此。如果未初始化,在哪里进行初始化以及如何进行初始化

在调用
getActionProxy
之后,我尝试了以下操作。但它仍然不起作用

ServletActionContext.setContext(proxy.getInvocation().getInvocationContext());
问题2:要设置会话,在
getActionProxy()
之前,我必须调用

ActionContext.getContext().setSession(createUserSession());
同样,在
getActionProxy

ActionContext actionContext = proxy.getInvocation().getInvocationContext();
actionContext.setSession(createUserSession());
设置会话。我想,这里有点不对劲

问题3:看起来,这里有几个上下文在起作用:
applicationContext
ActionContext
ServletContext
ServletActionContext

当我的测试类扩展
StrutsSpringTestCase
类时,我猜
applicationContext
已初始化。但我不确定其他情况。在哪里初始化它们

编辑:

对源代码的进一步调查揭示了一个问题。。 当我调用
ServletActionContext.getActionMapping()
时,在内部调用它的
ActionContext
get()方法

public Object get(String key) {
    return context.get(key);
}

context
是一个对象的映射,它在其中查找一个键的值
struts.actionMapping
,它不存在。因此,返回
null
。但不知道为什么会有。它不是空的。它还有其他键/值。

您的问题的答案:

ServletActionContext.getActionMapping()
返回操作上下文中的映射,如果未设置,则会得到
null

您不应该手动设置会话,会话是在执行操作时创建的

不要弄乱不同的类
ActionContext
ServletContext
ServletActionContext
。您不应该初始化这些对象,因为它是由超类
StrutsSpringTestCase
完成的

public void testGetActionMapping() {
    ActionMapping mapping = getActionMapping("/cInfo.action");
    assertNotNull(mapping);
    assertEquals("/", mapping.getNamespace());
    assertEquals("cInfo", mapping.getName());
}

public void testGetActionProxy() throws Exception {        
    ActionProxy proxy = getActionProxy("/cInfo.action");
    assertNotNull(proxy);

    CustInfo action = (CustInfo) proxy.getAction();
    assertNotNull(action);

    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
}

嗯。应用程序检查会话中的值。如果没有,则不会执行该操作。因此,我创建了一个会话并手动将其放入。这样看来,它可以处理“登录”/“会话”部分,但getActionMapping似乎不起作用。您应该为操作提供配置,即在
struts.xml
中。