Strut 2.3.1.2 Unit test, how to remove Spring codependency vs NPE with getContext()

a 夏天 提交于 2019-11-29 18:21:19

You should indeed keep your unit test as fast as possible (otherwise, no TDD). So you should do the minimum struts setup: (I'm using mockito and I have this code in a static block, full code in this 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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!