This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Unit Test
Skip to end of metadata
Go to start of metadata

Unit Test

Unit testing with Wicket is done by mocking the application. This means that you do not have a webserver running. This section will be about how to get started using unit testing with Wicket's testing framework.

Simple example

public class AgencyPageTest extends TestCase {

    public void testBasicRender() {
         WicketTester tester=new WicketTester();

Above example makes sure that your page can render. This in itself is a very powerful test & and should be the minimum of tests on all your pages (in this author's opinion).

Basic Test Case for some labels and elements ...

 * <p>
 * Note : This is a TestNG Test case, a JUNIT 4.x implementation should be similiar to do.
 * For more on testNG refer to <a href="">TestNG</a>.
 * Also, note that i have kept the underneath stuff to minimal logic, but more 
 * to show what the framework testing is like ...
 * </p>
 * Code for Actor Panel below is :<br/>
 * <code><br/>
 * public ActorPanel(String id) {<br/>
 *		super(id);<br/>
 * // add actor name<br/>
 *	add(new Label("actorId", "" + getActorData().getId()));<br/>
 *	add(new Label("actorName", getActorData().getActorName()));<br/>
 *	add(new Label("actorDepartment", getActorData().getDepartment()));<br/>
 *	add(new DropDownChoice("department", Arrays.<String>asList("HR","IS","FINANCE/ACCOUNTING")));<br/>
 * </code>
 * In HomePage class you do a simple add(new ActorPanel());<br/>
 * @author Vyas, Anirudh
public class HomePageTest {
	public WicketTester wicketTester = null;
	 * Basic Setup of our test 
	public void testSetup(){
		wicketTester  = new WicketTester();  // <-- A utility inside Wicket Framework for testing ...
		wicketTester.startPage(HomePage.class); // <--- You dont have to start a web server to test your stuff because of this ... =)
	 * Basic Home Page Test case for our application...
	public void testHomePage(){

		// $Id - Test Case 1.0 : ... is Home Page rendered?
		// $Id - Test Case 2.0 : Elements test cases ... ( Starts the Panel ... prepares a DummyPanel first for testing ).

		//$Id - Assertion ( Element - Label - actorDepartment ) --> Not Null
		//$Id - Assertion ( Element - Label - actorDepartment ) --> Same as "HR" 
		Assert.assertEquals(wicketTester.getTagByWicketId("actorDepartment").getValue(), "HR");

		//$Id - Assertion ( Element - Label - actorName ) --> Not Null
		// $Id - Assertion( Element - Label - actorName ) --> Same as "|| OM Gam Ganapataye Namaha ||"
		Assert.assertEquals(wicketTester.getTagByWicketId("actorName").getValue(), "|| OM Gam Ganapataye Namaha ||");

		//$Id - Assertion ( Element - DropdownChoice (select) - department ) --> Not Null 
                //$Id - Assertion ( Element - DropdownChoice (select) - department ) --> Not Null 
		DropDownChoice dChoice = (DropDownChoice) wicketTester.getComponentFromLastRenderedPage("panel:department"); // thanks Igor =)

		//$Id - Assertion ( Element - DropdownChoice (select) - department ) --> 3 values 



Note that in above test, should you have panel without a default constructor, then you'd have to do the following :

TestPanelSource testPanelSource = new TestPanelSource(){
			private static final long serialVersionUID = 1L;

			public Panel getTestPanel(String panelId){

				// prepare the SUT (which is Panel in this case)
				return new AssociateInformationPanel(panelId);

                // $Id - Assertion( Element - label - associateName) --> Assert label Values
		tester.assertLabel("panel:labelValueAssociateName", "LastName,FirstName");

Similiar to above, if you have a page without a default constructor; you would do the same; this time creating an anonymous ITestPageSource implementation.

In above test, as you can make out, first a WicketTester class is instantiated, This would create a DummyWebApplication instance, which will have new SessionStore, Response etc. and a DummyHomePage in it. A call to startPage method on wicket Tester initiates the cycle. First Mock request and response are setup,then a new RequestCycle is created followed by regular request processing (for details see Lifecycle section of documentation).

A note on paths : As in above assertLabel( ) we have to precede the actual label name or component name with container,in this case a panel.

  • No labels