在验收测试期间如何从Struts 2获取ActionContext? [英] How to get an ActionContext from Struts 2 during acceptance tests?

查看:132
本文介绍了在验收测试期间如何从Struts 2获取ActionContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以Struts 2和Tomcat为我的Servlet容器的应用程序上,使用Cucuming-jvm编写了接受测试(测试行为).在代码的某个时刻,我需要从由HttpServletRequest创建的Struts 2 HttpSession中获取用户.

I am writing acceptance tests (testing the behavior) using cucumber-jvm, on an application with Struts 2 and Tomcat as my Servlet Container. At some point in my code, I need to fetch the user from the Struts 2 HttpSession, created by an HttpServletRequest.

由于我正在测试并且没有运行Tomcat,因此我没有活动的会话,并且得到了NullPointerException.

Since I'm doing tests and not running Tomcat, I don't have an active session and I get a NullPointerException.

这是我需要调用的代码:

Here's the code I need to call:

public final static getActiveUser() {
    return (User) getSession().getAttribute("ACTIVE_USER");
}

和getSession方法:

And the getSession method:

public final static HttpSession getSession() {
    final HttpServletRequest request (HttpServletRequest)ActionContext.
                          getContext().get(StrutsStatics.HTTP_REQUEST);
    return request.getSession();
}

老实说,我对Struts 2知之甚少,所以我需要一点帮助.我一直在看这个带有嵌入式tomcat的黄瓜jvm示例,但是我很难理解.

In all honesty, I don't know much about Struts 2, so I need a little help. I've been looking at this cucumber-jvm with embedded tomcat example, but I'm struggling to understand.

我也一直在查看

I've also been looking at this Struts 2 Junit Tutorial. Sadly, it doesn't cover very well all the StrutsTestCase features and it's the simplest of use cases (all considered, a pretty useless tutorial).

那么,如何像用户正在使用应用程序一样运行验收测试?

So, how can I run my acceptance test as if the user was using the application?

感谢斯蒂芬·贝尼特斯(Steven Benitez)的答案!

Thanks to Steven Benitez for the answer!

我必须做两件事:

  1. 按照建议模拟HttpServletRequest,
  2. 模拟HttpSession以获得我想要的属性.

这是我添加到cumul-jvm测试中的代码:

here's the code I've added to my cucumber-jvm tests:

public class StepDefs {
    User user;
    HttpServletRequest request;
    HttpSession session;

    @Before
    public void prepareTests() {
        // create a user

        // mock the session using mockito
        session = Mockito.mock(HttpSession.class);
        Mockito.when(session.getAttribute("ACTIVE_USER").thenReturn(user);

        // mock the HttpServletRequest
        request = Mockito.mock(HttpServletRequest);
        Mockito.when(request.getSession()).thenReturn(session);

        // set the context
        Map<String, Object> contextMap = new HashMap<String, Object>();
        contextMap.put(StrutsStatics.HTTP_REQUEST, request);
        ActionContext.setContext(new ActionContext(contextMap));
    }

    @After
    public void destroyTests() {
        user = null;
        request = null;
        session = null;
        ActionContext.setContext(null);
    }

}

推荐答案

ActionContext是每个请求对象,代表操作在其中执行的上下文.静态方法getContext()setContext(ActionContext context)ThreadLocal支持.在这种情况下,您可以在测试前调用它:

An ActionContext is a per-request object that represents the context in which an action executes. The static methods getContext() and setContext(ActionContext context) are backed by a ThreadLocal. In this case, you can call this before your test:

Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put(StrutsStatics.HTTP_REQUEST, yourMockHttpServletRequest);
ActionContext.setContext(new ActionContext(contextMap));

然后使用以下命令清除它:

And then clean it up after with:

ActionContext.setContext(null);

此示例仅提供您要测试的方法.如果您需要根据此处未提供的代码在地图中添加其他条目,请相应地添加它们.

This example will only provide what the method you are testing needs. If you need additional entries in the map based on code you didn't provide here, then just add them accordingly.

希望有帮助.

这篇关于在验收测试期间如何从Struts 2获取ActionContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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