Java StrutsSpringTestCase-多个上下文-如何按顺序正确实例化它们
我正在为使用JUnit的Struts2、Spring和Hibernate的项目编写集成测试用例 我的测试类扩展了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(
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
中。