Running and testing the example
Posted on: February 4, 2011 at 12:00 AM
In this tutorial you will learn how to test the HelloWorld annotation example using Junit framework

Running And Testing The Example

An example of testing the HelloWorld Application using give below. This application uses the junit framework to test the application.


<%@ taglib prefix="s" uri="/struts-tags"%>
<title>Annotation Example</title>
<style type="text/css">
@import url(css/main.css);
.errorMessage {
color: green;
<body bgcolor="#A3A3FF">
<h2>Please Enter Your Name</h2>
<s:form action="hello">
<s:textfield name="userName" label="Please Specy Your Name" />
<s:submit />


<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Annotation Example</title>
<body bgcolor="#9E9EE8">

package net.roseindia.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class HelloWorldAction {
	private static final long serialVersionUID = 1L;
	String userName = "";
	String message;

	@Action(value = "/hello", results = {
			@Result(name = "error", location = "/jsp/index.jsp"),
			@Result(name = "success", location = "/jsp/successPage.jsp") })
	public String execute() throws Exception {
		if (getUserName().equals("")) {
			return "error";
		} else {
			message = "Hello " + userName;
			return "success";

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

	public String getUserName() {
		return userName;

	public void setUserName(String userName) {
		this.userName = userName;

package net.roseindia.test;

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

public class HelloWorldActionTest extends TestCase {
	String result;
	String classResult;
	HelloWorldAction testObject = new HelloWorldAction();

	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		result = "success";
		try {
			testObject.setUserName("Alex Ferrioda");
			classResult = testObject.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block

	public void testExecute() {

	protected void tearDown() throws Exception {
		// TODO Auto-generated method stub

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


Download Select Source Code

Related Tags for Running and testing the example :