Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The recommended way to test actions is to instantiate the action classes and test them. The JUnit Plugin supports testing actions within a Struts invocation, meaning that a full request is simulated, and the output of the action can be tested.

Struts actions (without Spring)

To test actions that do not use Spring, extend StrutsTestCase. The following example shows different ways of testing an action:
Mapping:

...

Field

Description

MockHttpServletRequest request

the requestthat will be passed to Struts. Make sure to set parameters in this object before calling methods like getActionProxy

MockHttpServletResponse response

the response object passed to Struts, you can use this class to test the output, response headers, etc

MockServletContext servletContext

the servlet context object passed to Struts

Struts Actions using Spring

If you use Spring as the object factory, the StrutsSpringTestCase class can be used to write your JUnits. This class extends StrutsTestCase and has a applicationContext field of type ApplicationContext.

...