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(); tester.startPage(myPage.class); tester.assertRenderedPage(myPage.class); } }
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="http://testng.org/doc/">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/> *}<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 */ @BeforeClass 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 ... bye bye HTMLUnit =) } /** * Basic Home Page Test case for our application... */ @Test public void testHomePage(){ // $Id - Test Case 1.0 : ... is Home Page rendered? wicketTester.assertRenderedPage(HomePage.class); // $Id - Test Case 2.0 : Elements test cases ... ( Starts the Panel ... prepares a DummyPanel first for testing ). wicketTester.startPanel(ActorPanel.class); System.out.println(wicketTester.getApplication().getHomePage()); //$Id - Assertion ( Element - Label - actorDepartment ) --> Not Null Assert.assertNotNull(wicketTester.getTagByWicketId("actorDepartment").getValue()); //$Id - Assertion ( Element - Label - actorDepartment ) --> Same as "HR" Assert.assertEquals(wicketTester.getTagByWicketId("actorDepartment").getValue(), "HR"); //$Id - Assertion ( Element - Label - actorName ) --> Not Null Assert.assertNotNull(wicketTester.getTagByWicketId("actorName").getValue()); // $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 Assert.assertNotNull(wicketTester.getTagByWicketId("department").getValue()); System.out.println("pane:department (component name is preceeded by panel ... as you might notice ----- > " + wicketTester.getComponentFromLastRenderedPage("panel:department")); } }
More to follow ...