Versions Compared

Key

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

The example code for this tutorial, Unit_Testing_Struts2_Mvnunit_testing, is available on Google Code - httpat https://code.googlegithub.com/papache/struts2-examples/downloads/listImage Removed. 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.struts-examples

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.

...

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).

Info

Note that the Struts 2 JUnit plugin can be used to design unit tests of other Action class methods such as the input method and also to test methods of a custom interceptor you add to the interceptor stack. Also in this example, the test is for validation performed in the validate method. But the same type of test would work if the validation was done using XML file validation.

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.

Code Block
XMLxmlXML
xml
titlestruts.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>

...

The input fields for the form have the following name values: personBean.firstName, personBean.lastName, personBean.email, and personBean.age. When the user fills out those fields Struts will take the values and provide them to the appropriate set methods of the personBean object. So as part of the test I need to simulate the user filling out these form fields. The StrutsTestCase provides a request object (of type MockHttpServletRequest) that I can use to set these values in the request scope.

Code Block
XMLjavaXML
java
titletestExecuteValidationPasses method from RegisterTest class


@Test
public void testExecuteValidationPasses() throws Exception() {

  request.setParameter("personBean.firstName", "Bruce");

  request.setParameter("personBean.lastName", "Phillips");
		
  request.setParameter("personBean.email", "bphillips@ku.edu");
		
  request.setParameter("personBean.age", "19");

  ActionProxy actionProxy = getActionProxy("/register.action");

  Register action = (Register) actionProxy.getAction() ;

  assertNotNull("The action is null but should not be.", action);

  String result - actionProxy.execute();

  assertEquals("The execute method did not return " + ActionSupport.SUCCESS + " but should have.", ActionSupport.SUCCESS, result);
  
}


The first statements in the test method use the request object to set the values of any request parameters. These simulate the values the user would enter into the form fields. Note how the first argument to setParameter is the same as the value of the name attribute in the Struts textfield tag in the register.jsp page.

...

To test that validation should fail, I just need to have a test method that doesn't provide input for a form field. For example, in the validate method of the Register Action class, is a test to ensure the user has entered some information for the personBean.firstName input field. In the test method I would just not use the request object to set a parameter for that field.

Code Block
XMLjavaXML
java
titletestExecuteValidationFailsMissingFirstName method from RegisterTest class


@Test
public void testExecuteValidationFailsMissingFirstName() throws Exception() {

  //request.setParameter("personBean.firstName", "Bruce");

  request.setParameter("personBean.lastName", "Phillips");
		
  request.setParameter("personBean.email", "bphillips@ku.edu");
		
  request.setParameter("personBean.age", "19");

  ActionProxy actionProxy = getActionProxy("/register.action");

  Register action = (Register) actionProxy.getAction() ;

  assertNotNull("The action is null but should not be.", action);

  String result - actionProxy.execute();

  assertEquals("The execute method did not return " + ActionSupport.INPUT + " but should have.", ActionSupport.INPUT, result);
  
}


In the last assertEquals statement my test checks that the Struts 2 framework returned "input" as that is what the Struts 2 framework will return if the validation adds a field or action error.

...