You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

The example code for this tutorial, Unit_Testing_Struts2_Mvn, is available on Google Code - http://code.google.com/p/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Unit_Testing_Struts2_Mvn. In that folder will be a README.txt file with instructions on now to build and run the example application. This tutorial assumes you already know how to write unit tests using Junit.

Introduction

Struts 2 supports running unit tests of methods in the Struts Action class with the Struts 2 JUnit plugin. The JUnit plugin allows you to test methods of an Action class from within the Struts 2 framework. The Struts Servlet filter and interceptors fire just as if your application was running on a Servlet container.

The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.

Setup

The Struts 2 JUnit plugin jar file must be on your application's class path. In the example application (see info above) the pom.xml includes a dependency for the struts2-junit-plugin. There are numerous transitive dependencies, including to JUnit and the Spring framework.

Writing A Unit Test

In the example application, the Register Action class includes using the validate method. This method is automatically executed by the Struts 2 framework prior to the execute method. Additionally, this method needs the values from the user's input on the form to already have been provided to the instance fields of the Action class (this work is done by another Struts 2 interceptor). So it would be difficult to test the validate method without the overall Struts 2 framework running.

To use the Struts 2 plugin to ensure the Strut 2 framework runs as part of the test, you need to have your JUnit test class extend StrutsTestCase (see RegisterTest class in the example application).

To test the validate method we want Struts to call the Struts action that will cause the Action class's validate and execute methods to be run. In the example application this action is register.

struts.xml


	  <action name="register" class="org.apache.struts.register.action.Register" method="execute">
		<result name="success">/thankyou.jsp</result>
		<result name="input">/register.jsp</result>
	  </action>

Remember the validate method will be called automatically by the framework before calling the execute method. If validation fails the Struts framework will return "input". If there are no validation errors then the framework will call the execute method and return whatever String the execute method returns.

Test Validation Should Pass

For our first test we'll test that there should be no validation errors. In the normal flow of this application the user would first enter the form data shown on the register.jsp page

  • No labels