I'm just upgrading to Struts 2.3.1.2, and having a few issues with JUnit tests.
My old test code was like this....
public class HomeActionTest {
@Test
public void testUserNameErrorMessage() throws Exception {
HomeAction action = new HomeAction();
setupMocks(action);
action.execute();
}
}
The action class's execute method has the code
String text = getText(GLOBAL_SELECT);
This causes a NullPointerExeception though in LocalizedTextUtil since it calls...
ActionContext.getContext()
Now I could try and do this...
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....
I follow the new Struts 2.3.x.x testing doc
My struts.xml is wired with spring.
<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>
My Test class
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.
My problem is that this isn't a UNIT test class, its now making calls all the way down to the DB layer since that's how its wired for run-time.
So here is my question, I know I've taken a while to get to it, is it possible to get an Action class that has access to resources it needs (for method calls such as getText()), but is not all Spring wired up?
For the moment I'm tempted to go the refection route to remove all methods that match setServicexxxx on my action, at least then I'd get a NullPointerException on running the test and I can mock out that service, but that's just wrong. I want my tests to be FAST, not spend 2 seconds starting up the Spring context.
How did Struts2.3 end up with a Base test class that doesn't follow the mantra of what a Unit test is?
This was all fine in Struts 2.0.9 (xwork-2.0.3.jar).
What am I missing?
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);
来源:https://stackoverflow.com/questions/10756591/strut-2-3-1-2-unit-test-how-to-remove-spring-codependency-vs-npe-with-getcontex