Struts ActionContext在测试期间是null。
使用Struts2 JUnit插件,我进行了以下测试:
public class MainActionIT extends StrutsJUnit4TestCase
{
@Test
public void testAction() {
Map<String, Object> application = new HashMap<String, Object>();
application.put("options","home");
ActionContext.getContext().put("application",application);
ActionProxy proxy = getActionProxy("/home");
String result = proxy.execute();
}
}这两个相关的班级如下:
public class MainAction extends BaseAction
{
@Action(value = "/home", results = {@Result(name = "success", location = "home.jsp")})
public String getHome()
{
Map options = getHomeOptions();
return SUCCESS;
}
}
public class BaseAction extends ActionSupport
{
public Map getHomeOptions()
{
return ActionContext.getContext().get("application").get("options");
}
}我试图用一个"application"来模拟ActionContext的HashMap对象。
值在测试中设置,但一旦代码在BaseAction中执行,则值为null。这里有类似的问题(link),但在我的情况下答案是不正确的。
是否正在创建不同的ActionContext?如果是,那么如何将一个变量传递给BaseAction
发布于 2014-08-22 08:40:05
ActionContext是在操作执行期间创建的。你应该检查这段代码来证明这个概念。
@Test
public void shouldAdditionalContextParamsBeAvailable() throws Exception {
// given
String key = "application";
assertNull(ActionContext.getContext().get(key));
// when
String output = executeAction("/home");
// then
assertNotNull(ActionContext.getContext().get(key));
}
@Override
protected void applyAdditionalParams(ActionContext context) {
Map<String, Object> application = new HashMap<String, Object>();
application.put("options","home");
context.put("application", application);
}关于模板
可以在子类中覆盖
applyAdditionalParams(ActionContext),以提供操作调用期间使用的附加参数和设置。
https://stackoverflow.com/questions/25436615
复制相似问题