Unit Testing with StrutsTestCase for JUnit Tutorial

As the name implies, StrutsTestCase is a tool for testing your Struts code. In this article, I’ll cover:

  • What is StutsTestCase?
  • How?
  • When?
  • Where?

This article assumes the reader has familiarity and experience with Junit.

“StrutsTestCase for JUnit is an extension of the standard JUnit TestCase class that provides facilities for testing code based on the Struts framework. StrutsTestCase provides both a Mock Object approach and a Cactus approach to actually run the Struts ActionServlet, allowing you to test your Struts code with or without a running servlet engine.” -

In this article, we’ll discuss and examples using the MockObject approach; not Cactus.

StrutsTestCase reads the struts config file and loads up validators, plug-ins, forwards, etc. In other words, it can mock what happens when a servlet container is started without actually requiring a servlet container.

Just like JUnit – Extend a Class and follow a method naming convention

You have the same access to setUp() and tearDown():

public class TestLoginAction extends MockStrutsTestCase {
    public void setUp() { super.setUp(); }
    public void tearDown() { super.tearDown(); }
}

Extends JUnit’s TestCase, so access to all the assert*. Differentiates itself by using verify* methods.

Form validation example:

public void testNonNumberEntry() {
 
	addRequestParameter("txtFlatPercent", "arg1"); //add bogus percent
    	setRequestPathInfo("/calculate");
 
    	CalculatorValidatorActionForm form = new CalculatorValidatorActionForm();
    	form.setTxtName("Account Name");
    	form.setChkOptionRate("Flat");
    	form.setTxtAccountVal("55000");
    	form.setTxtFixedPer("string");
    	setActionForm(form);
 
    	actionPerform();
 
    	verifyActionErrors(new String[] { "prompt.txtFlatPercent"});
 
	}
 
 
    public void testFlatPercentValidationWithOutExceptionRequest() {
 
    	setRequestPathInfo("/calculate");
 
    	CalculatorValidatorActionForm form = new CalculatorValidatorActionForm();
    	form.setTxtName("Account Name");
    	form.setChkOptionRate("Flat");
    	form.setTxtFlatPercent("49");
    	form.setTxtAccountVal("500000");
    	setActionForm(form);
 
    	actionPerform();
 
    	verifyForward("flat");
    	assertNull(getMockRequest().getSession().getAttribute(ConstantsInterface.CONFIRM_EXCEPTION_REQUIRED));
 
    }

Struts Plug-In Validation

try {
	FeeCalcServiceFactory serviceFactory = (FeeCalcServiceFactory)getActionServlet().getServletContext().getAttribute(ConstantsInterface.FEECALC_SERVICE_FACTORY_KEY);
	assertNotNull(serviceFactory.createService());
  } catch (Exception e) {
	log.error(e);
	fail("testFactory - should not have thrown exception when obtaining service");
  }
}
 
public void testGetRates() {
   try {
	FeeCalcServiceFactory serviceFactory = (FeeCalcServiceFactory)getActionServlet().getServletContext().getAttribute(ConstantsInterface.FEECALC_SERVICE_FACTORY_KEY);
	FeeCalcService service = serviceFactory.createService();
	// should never be empty
	assertTrue(service.getRates().size() >= 0);
 
   } catch (Exception e) {
	log.error(e);
	fail("testGetRates - should not have thrown exception");
   }
}

StrutsTestCase Compatibility:

  • Struts 1.2, 1.1
  • Tiles
  • Sub-applications
  • Java Servlet 2.2, 2.3, and 2.4 specifications
  • JUnit 4.0

StrutsTestCase Gotchas:

  • WEB-INF - “By default, the Struts ActionServlet will look for the file WEB-INF/struts-config.xml, so you must place the directory that contains WEB-INF in your CLASSPATH. If you would like to use an alternate configuration file, please see the setConfigFile() method for details on how this file is located.”
  • Does not support Struts 1.3 or 2

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

ServiceCycle is a registered trademark of Supergloo, inc..