Versions Compared

Key

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

The example code for this tutorial, unit_testing, is available for checkout at https://svngithub.com/apache.org/repos/asf/struts/sandbox/trunk/struts2examples/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.

...

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

...

Code Block
java
titletestExecuteValidationPasses method from RegisterTest class
java


@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);
  
}

...

Code Block
java
titletestExecuteValidationFailsMissingFirstName method from RegisterTest class
java


@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);
  
}

...