English 中文(简体)
2.3.1.2 单元测试,如何用CetContext()来去除春季代码依赖与 NPE的 NPE 。
原标题:Strut 2.3.1.2 Unit test, how to remove Spring codependency vs NPE with getContext()

我只是升级到Struts 2.3.1.2, 和JUM测试有一些问题。

我的旧测试代码是这样的...

public class HomeActionTest  {

    @Test
    public void testUserNameErrorMessage() throws Exception {
     HomeAction action = new HomeAction();
     setupMocks(action);
     action.execute();
    }
}

动作组执行方法有代码

    String text = getText(GLOBAL_SELECT);

虽然在本地化的TextUtil 中,这引起了一个 NullPointer 例外,因为它叫...

   ActionContext.getContext()

现在我可以尝试做这个...

  ActionContext.setContext(new ActionContext(new HashMap()));

But then I ll get a NullPointerException since the Valuestack from the context is null. I could go on and on trying to fix these problems, but it seems like a futile task. There must be a better way?!?

这个嘛... Well...

我跟着新的"http://strutts.apache.org/2.2.3/docs/struts-2-junit-plugin-tutial.html" rel=“nofollow” >Struts 2.3.x.x测试 doc

我的臀部有弹簧

 <action name="secure/home" class="homeAction" method="execute">
            <result>home.jsp</result>
            <result name="input">home.jsp</result>
        <result name="error">error.jsp</result>
 </action>

我的考试班

public class HomeActionTest extends StrutsSpringTestCase   {

    @Test
    public void testUserNameErrorMessage() throws Exception {
        ActionProxy proxy = getActionProxy("/secure/home");

        // setup the session
        Map<String, Object> session = new HashMap<String, Object>();  
        ActionContext actionContext = proxy.getInvocation().getInvocationContext();  
        actionContext.setSession(session);  

        HomeAction homeAction = (HomeAction) proxy.getAction();

        proxy.execute();
    }
}

But this now means I get a fully populated Spring injected Action class! Most people would saying "YEA!!!" at this point.

我的问题是,这不是UNIT测试级, 它现在一直打电话到 DB 层, 因为它的电线是如何 运行时间。

所以,这就是我的问题,我知道我花了一小段时间才找到它, “强势”是有可能得到一个“行动集团”的,该“行动集团”可以获得它所需要的资源(例如“Text()”等方法电话),但“春天”并没有全部连接起来吗?“/强”

此刻,我试图通过复选途径去删除所有方法, 来匹配Servicexxxxet的动作, 至少这样我就可以在测试时得到一个 NullPointer 例外, 我可以嘲笑这个服务, 但这是不对的。 我希望我的测试是FAST, 不要花两秒钟启动春季背景。

Struts2.3怎么会有基地测试等级 却不遵循单位测试的咒语呢?

在Struts 2.0.9(xwork-2.0.3.jar)中,这一切都很好。

我错过了什么?

最佳回答

您确实应该尽可能快地保持您的单位测试( 或者说, 没有 TDD ) 。 所以您应该做最起码的设置 :( 我使用 modito 和我在静态区块里有这个代码, 完整的代码在这个 < a href= > 中 。 https:// gist. github.com/ 2948345" rel= “ no follow” >gist )

ActionContext actionContext = mock(ActionContext.class);
ServletContext servletContext = mock(ServletContext.class);
when(actionContext.getLocale()).thenReturn(Locale.FRENCH);
ValueStack valueStack = mock(ValueStack.class);
Map<String, Object> context = new HashMap<String,Object>();
Container container = mock(Container.class);
XWorkConverter conv = mock(XWorkConverter.class);
when(container.getInstance(XWorkConverter.class)).thenReturn(conv);
when(conv.convertValue(any(Map.class), any(Object.class), any(Class.class))).thenAnswer(new Answer<Object>() {
    public Object answer(InvocationOnMock invocation) throws Throwable {
        log.info(invocation.getArguments()[1].toString());
        return "VALUE";
    }

});
context.put(ActionContext.CONTAINER, container);
when(valueStack.getContext()).thenReturn(context);
when(actionContext.getValueStack()).thenReturn(valueStack);
ServletActionContext.setContext(actionContext);
ServletActionContext.setServletContext(servletContext);
问题回答

暂无回答




相关问题
array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

How can I determine Objects in application context?

I am trying to write a portlet for Liferay (using Tomcat and Spring) and need to use a database via Persistence API/Hibernate. I am using some configuration XMLs (applicationContext.xml, etc.) and ...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

热门标签