Home Struts Struts Struts2.2.1 Test Actions

Ask Questions?

View Latest Questions


 
 

Test Actions
Posted on: February 3, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn how to test the struts action using jnit

Test Actions

An example of Testing a struts Action is given below using the junit. In this example the execute method is being test.

HelloAction.java

package net.roseindia.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		return SUCCESS;
	}
}

HelloActionTest.java

package net.roseindia.test;

import org.junit.Test;

import junit.framework.TestCase;
import net.roseindia.action.HelloAction;

import com.opensymphony.xwork2.ActionSupport;

public class HelloActionTest extends TestCase {

	HelloAction helloAction = new HelloAction();
	String result;

	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		result = helloAction.execute();
		super.setUp();
	}

	@Test
	public void testExecute() {
		try {
			result = helloAction.execute();
		} catch (Exception e) {
			e.toString();
		}
		assertTrue(ActionSupport.SUCCESS.equals(result));
	}

	@Override
	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub
		super.tearDown();
	}

}

SampleInterfaceImp.java

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />

<action name="testAction" class="net.roseindia.action.HelloAction">
<result name="success">/resources/HelloWorld.jsp</result>
</action>

</package>
<!-- Add packages here -->

</struts>

When you run this application it will display message as shown below:


 
 

Download Select Source Code

Related Tags for Test Actions :


DMCA.com