[英]Struts2 JUnit ActionContext objects
測試期間 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");
}
}
我正在嘗試使用HashMap
模擬ActionContext
的"application"
對象。
值是在測試中設置的,但是一旦代碼在BaseAction
執行,值就是null
。 這里有類似的問題( 鏈接),但在我的情況下答案不正確。
是否創建了不同的ActionContext
? 如果是這樣,如何將變量傳遞給BaseAction
?
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)
可以在子類中覆蓋以提供在動作調用期間使用的額外參數和設置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.