启用图块后,StrutsTestCase中的NPE [英] NPE in StrutsTestCase after enabling Tiles

查看:53
本文介绍了启用图块后,StrutsTestCase中的NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一些扩展org.apache.struts2.StrutsTestCase的JUnit测试.我在struts.apache上使用了教程. org作为我的起点.

I developed some JUnit tests that extend org.apache.struts2.StrutsTestCase. I used the tutorial on struts.apache.org as my starting point.

一切正常,直到我修改了简单的Web应用程序以使用Tiles.我的Tiles在该应用中正常运行,但现在我的Action测试用例已停止工作.

Everything was working fine until I modified my simple web application to use Tiles. I have Tiles working fine in the app but now my Action test cases have stopped working.

当我运行以下代码行时,我在org.apache.struts2.views.tiles.TilesResult.doExecute上获取NullPointerException:

I'm getting NullPointerException at org.apache.struts2.views.tiles.TilesResult.doExecute when I run the following line of code:

ActionProxy proxy = getActionProxy("/displaytag.action");

日志显示Struts 2 Action成功执行,直到尝试将其传递给TilesResult.doExecute.

The log shows the Struts 2 Action is executing succesfully until it tries to hand it off to TilesResult.doExecute.

我怀疑这是因为测试在容器外部运行,并且只在web.xml中引用了tiles.xml,因此我的StrutsTestCase测试不知道在tile.xml中的定义位置.

I suspect it is because the tests run outside of the container and the tiles.xml is only referenced in the web.xml and therefore my StrutsTestCase tests don't know where to find the definitions in tiles.xml.

这有意义吗?

我正在使用Struts 2.2.1.1以及Struts发行版中包含的与瓷砖相关的jars(2.0.6版).

I'm using Struts 2.2.1.1 and the tiles related jars (v. 2.0.6) included in the Struts distribution.

我将在StrutsTestCase中包含一个代码段,但请注意,当我从Tomcat中的浏览器运行该应用程序时,所有程序都可以成功运行,只有当我在Tomcat之外运行StrutsTestCase时,该程序才会失败.在添加Tiles之前,测试用例成功运行.

I'll include a code snippet from my StrutsTestCase but please note everything runs successfully when I run the app from the browser in Tomcat, it only fails when I run the StrutsTestCase outside of Tomcat. And the test cases ran successfully before I added 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一起使用的教程页面,但是 SourceForge页面还没有'从Struts 1.3开始进行了更新.此外,StrutsTestCase和MockStrutsTestCase有什么区别

Lastly, can anyone explain what the deal is with StrutsTestCase? There's a tutorial page for using it with Struts 2 on struts.apache.org but the SourceForge page for it hasn't been updated since Struts 1.3 Also, what's the difference between StrutsTestCase and MockStrutsTestCase

推荐答案

我想您正在使用侦听器初始化图块:

I imagine you're initialising tiles with a listener:

<listener>
    <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>

您需要在测试中初始化该监听器.我发现了其他一些具有相同问题的问题[1]. 下面的代码在您的扩展StrutsSpringTestCase的类中.您需要覆盖setupBeforeInitDispatcher.在下面的代码片段中,重写将设置applicationContext属性(如果使用spring时也需要),并初始化Tiles(在if(tilesApplication)段中,tilesApplication是一个布尔值,因此您可以基于您的应用程序是否与tile一起运行):

You need to initialise that Listener in your tests. I found a few others with the same issue [1]. The code below is in your class that extends StrutsSpringTestCase. You need to override the setupBeforeInitDispatcher. In the code snippet below, the override sets the applicationContext attribute (also needed if you're using spring) and initialises Tiles (inside the if(tilesApplication) segment, where tilesApplication is a boolean so you can toggle this code on an off based on your whether or not your application runs with tiles ):

    /** Overrides the previous in order to skip applicationContext assignment: context is @autowired
 * @see org.apache.struts2.StrutsSpringTestCase#setupBeforeInitDispatcher()
 **/
@Override
protected void setupBeforeInitDispatcher() throws Exception {
    //init context

    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);

    if(tilesApplication){
        servletContext.addInitParameter(BasicTilesContainer.DEFINITIONS_CONFIG, "WEB-INF/tiles.xml");
        final StrutsTilesListener tilesListener = new StrutsTilesListener();
        final ServletContextEvent event = new ServletContextEvent(servletContext);
        tilesListener.contextInitialized(event);
    }

}

[1]参见 http ://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit/

这篇关于启用图块后,StrutsTestCase中的NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆