本文介绍了在StrutsJUnit4TestCase期间,ActionContext.getContext().getParameters()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Maven运行JUnit测试,其中正在测试Struts动作Java方法,该方法将进行以下调用:

I am running a JUnit test via maven where a struts action java method is being tested that makes the following call:

// Gets this from the "org.apache.struts2.util.TokenHelper" class in the struts2-core jar
String token = TokenHelper.getTokenName();

这是"TokenHelper.java"中的方法:

Here is the method in "TokenHelper.java":

/**
 * Gets the token name from the Parameters in the ServletActionContext
 *
 * @return the token name found in the params, or null if it could not be found
 */
public static String getTokenName() {

    Map params = ActionContext.getContext().getParameters();

    if (!params.containsKey(TOKEN_NAME_FIELD)) {
        LOG.warn("Could not find token name in params.");

        return null;
    }

    String[] tokenNames = (String[]) params.get(TOKEN_NAME_FIELD);
    String tokenName;

    if ((tokenNames == null) || (tokenNames.length < 1)) {
       LOG.warn("Got a null or empty token name.");

        return null;
    }

    tokenName = tokenNames[0];

    return tokenName;
}

此方法的第一行返回null:

Map params = ActionContext.getContext().getParameters();

下一个LOC代码"params.containKey(...)"将引发NullPointerException,因为"params"为空.

The next LOC down, "params.containKey(...)" throws a NullPointerException because "params" is null.

正常调用此操作时,此命令运行正常.但是,在JUnit测试期间,会出现此空指针.

When this action is called normally, this runs fine. However, during the JUnit test, this Null Pointer occurs.

我的测试类如下:

@Anonymous
public class MNManageLocationActionTest extends StrutsJUnit4TestCase {

    private static MNManageLocationAction action;

    @BeforeClass
    public static void init() {
        action = new MNManageLocationAction();
    }

    @Test
    public void testGetActionMapping() {
        ActionMapping mapping = getActionMapping("/companylocation/FetchCountyListByZip.action");
        assertNotNull(mapping);
    }

    @Test
    public void testLoadStateList() throws JSONException {
        request.setParameter("Ryan", "Ryan");

        String result = action.loadStateList();

        assertEquals("Verify that the loadStateList() function completes without Exceptions.", 
              result, "success");
    }
}

在我切换为使用StrutsJUnit4TestCase之后,ActionContext.getContext()至少不再为null.

The ActionContext.getContext() is at least no longer null after I switched to using StrutsJUnit4TestCase.

知道为什么.getParameters()返回null吗?

Any idea why .getParameters() is returning null?

推荐答案

您需要在测试方法中自行初始化参数映射.另外,如果要获取令牌名称,则需要将其放入参数映射中.

You need to initialize parameters map by yourself inside your test method. Additionally if you want to get token name you need to put it in parameters map.

Map<String, Object> params = new HashMap<String, Object>();
params.put(TokenHelper.TOKEN_NAME_FIELD, 
                       new String[] { TokenHelper.DEFAULT_TOKEN_NAME });
ActionContext.getContext().setParameters(params);

这篇关于在StrutsJUnit4TestCase期间,ActionContext.getContext().getParameters()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 11:06