我開發了一些擴展org.apache.struts2.StrutsTestCase的JUnit測試。我使用struts.apache.org上的tutorial作爲我的出發點。在啓用Tiles之後的StrutsTestCase中NPE
一切工作正常,直到我修改我的簡單的Web應用程序使用瓷磚。我的Tiles在應用程序中工作正常,但現在我的Action測試用例已停止工作。
我在org.apache.struts2.views.tiles.TilesResult.doExecute得到NullPointerException異常,當我運行下面的代碼行:
ActionProxy proxy = getActionProxy("/displaytag.action");
日誌顯示了Struts 2動作被成功地執行,直到它會嘗試將其交給TilesResult.doExecute。
我懷疑這是因爲測試運行在容器之外,並且tiles.xml僅在web.xml中引用,因此我的StrutsTestCase測試不知道在tiles.xml中的何處找到定義。
這是有道理的嗎?
我正在使用Struts 2.2.1.1和Struts發行版中包含的與tiles相關的jar(v.2.0.6)。
我會在我的StrutsTestCase中包含代碼片段,但請注意,當我在Tomcat中的瀏覽器中運行應用程序時,所有內容都能成功運行,但只有在Tomcat之外運行StrutsTestCase時纔會失敗。在添加Tiles之前,測試用例已成功運行。
public class TagActionTest extends StrutsTestCase {
static Logger logger = Logger.getLogger(TagActionTest.class);
public void testCreateTagFail() throws Exception {
logger.debug("Entering testCreateTagFail()");
try {
request.setParameter("name", "");
ActionProxy proxy = getActionProxy("/createtag.action");
TagAction tagAction = (TagAction) proxy.getAction();
proxy.execute();
assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present", tagAction.getFieldErrors().size() == 1);
assertTrue("Problem field 'name' not present in fieldErrors but it should have been",
tagAction.getFieldErrors().containsKey("name"));
} catch (Exception e) {
logger.debug("Error running testCreateTagFail()");
e.printStackTrace();
assertTrue("Error running testCreateTagFail()", false);
}
}
部分堆棧跟蹤:
java.lang.NullPointerException
at org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:373)
最後,任何人都可以解釋協議是與StrutsTestCase的是什麼?有一個教程頁面用於struts.apache.org上的Struts 2,但是從Struts 1.3開始,SourceForge page for it還沒有更新。另外,StrutsTestCase和MockStrutsTestCase之間的區別是什麼
我迷迷糊糊翻過了同樣的問題之前添加以下行
。賈斯汀,你是否設法解決並解決它? – mmalmeida 2012-01-27 17:24:57
@wild_oscar,不幸的是,沒有。祝你好運 – Justin 2012-01-28 01:49:14
@Justin找到了解決辦法? – 2013-12-04 14:58:54