Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

To use the Struts 2 JUnit plugin, you'll need to have the plugin's Jar file on your application's class path.  If you're using Maven you can add this dependency to your pom.xml.

Code Block
XML
XML
titleStruts 2 JUnit Plugin DependencyXML
<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-junit-plugin</artifactId>
    <version>STRUTS_VERSION</version>
</dependency>

...

For this tutorial we'll use the following Struts 2 ActionSupport class.

Code Block
JAVA
JAVA
titleAccountAction.javaJAVA
public class AccountAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private static final Logger logger = Logger.getLogger( AccountAction.class.getName() );

	private Account accountBean;

	public String execute(){

		return SUCCESS;

	}

	public void validate(){

		logger.debug("In method validate. accountBean's state is " + accountBean );

		if ( accountBean.getUserName().length() == 0 ){

			addFieldError( "accountBean.userName", "User name is required." );

		}

		if ( accountBean.getUserName().length() < 5 ) {

			addFieldError( "accountBean.userName", "User name must be at least 5 characters long." );

		}

		if ( accountBean.getUserName().length() > 10 ) {

			addFieldError( "accountBean.userName", "User name cannot be at more than 10 characters long." );

		}


	}

	public Account getAccountBean() {
		return accountBean;
	}

	public void setAccountBean(Account accountBean) {
		this.accountBean = accountBean;
	}

}


...

To test the validate method we can use the Struts 2 JUnit plugin to simulate the Servlet container and Struts 2 framework. Our Junit test class just needs to extend the StrutsTestCase. The StrutsTestCase class is part of the Strut 2 JUnit Plugin library.

Code Block
JAVA
JAVA
titleJUnit Test Case For Struts Action ClassJAVA
public class TestAccountActionUsingStrutsTestCase extends StrutsTestCase {

    public void testUserNameErrorMessage() throws Exception {

    	request.setParameter("accountBean.userName", "Bruc");
    	request.setParameter("accountBean.password", "test");

    	ActionProxy proxy = getActionProxy("/createaccount");

    	AccountAction accountAction = (AccountAction) proxy.getAction();

        proxy.execute();

        assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present", accountAction.getFieldErrors().size() == 1);
		assertTrue("Problem field account.userName not present in fieldErrors but it should have been",
				accountAction.getFieldErrors().containsKey("accountBean.userName") );

    }

    public void testUserNameCorrect() throws Exception {

    	request.setParameter("accountBean.userName", "Bruce");
    	request.setParameter("accountBean.password", "test");

    	ActionProxy proxy = getActionProxy("/createaccount");

    	AccountAction accountAction = (AccountAction) proxy.getAction();

        String result = proxy.execute();

        assertTrue("Problem There were errors present in fieldErrors but there should not have been any errors present", accountAction.getFieldErrors().size() == 0);
        assertEquals("Result returned form executing the action was not success but it should have been.", "success", result);

    }

}


...

We then create a proxy of the createaccount action. This action is defined in struts.xml as follows.

Code Block
JAVA
JAVA
titlestruts.xmlJAVA
<action name="createaccount" class="edu.ku.it.si.struts2_junit_example.action.AccountAction">
	<result>/thankyou.jsp</result>
	<result name="input">/createaccount.jsp</result>
</action>

...

Override getConfigPath method to return a comma separated list of paths to a configuration file.

Code Block
JAVA
JAVA
titleSpecify Struts Configuration File Location ExampleJAVA
    @Override
    protected String getConfigPath() {
        return "struts-test.xml";
    }

...

protected java.lang.String getContextLocations()

For example:

Code Block
Java
Java
titleSpecify Spring Configuration File Location ExampleJava

@Override
public String getContextLocations() {
		
  return "edu/ku/it/si/tutorial/action/TestAccountAction-context.xml";

}