uction page public class MyPage extends WebPage { public MyPage() { add(new Label("myMessage", "Hello!")); add(new Link("toYourPage") { public void onClick() { setResponsePage(new YourPage("Hi!")); } }); } }
// test code private WicketTester tester; public void setUp() { tester = new WicketTester(); } public void testRenderMyPage() { //start and render the test page tester.startPage(MyPage.class); //assert rendered page class tester.assertRenderedPage(MyPage.class); //assert rendered label component tester.assertLabel("myMessage", "Hello!"); }
The above example is straight forward: start
MyPage.class
and assert
Label
it rendered. Next, we try to navigate through a
Link
:
// production page public class YourPage extends WebPage { public YourPage(String message) { add(new Label("yourMessage", message)); info("Wicket Rocks ;-)"); } } //test code public void testLinkToYourPage() { tester.startPage(MyPage.class); //click link and render tester.clickLink("toYourPage"); tester.assertRenderedPage(YourPage.class); tester.assertLabel("yourMessage", "Hi!"); }
tester.clickLink(path);
will simulate user click on the component (in this case, it's a
Link
) and render the response page
YourPage
. Ok, unit test of
MyPage
is completed. Now we test
YourPage
standalone:
//test code public void testRenderYourPage() { // provide page instance source for WicketTester tester.startPage(new TestPageSource() { public Page getTestPage() { return new YourPage("mock message"); } }); tester.assertRenderedPage(YourPage.class); tester.assertLabel("yourMessage", "mock message"); // assert feedback messages in INFO Level tester.assertInfoMessages(new String[] {"Wicket Rocks ;-)"}); }
Instead of
tester.startPage(pageClass)
, we define a {@link org.apache.wicket.util.tester.ITestPageSource} to provide testing page instancefor
WicketTester
. This is necessary because
YourPage
uses a custom constructor, which is very common for transferring model data, but cannot be instantiated by reflection. Finally, we use
assertInfoMessages
to assert there is a feedback message "Wicket Rocks ;-)" at the INFO level. TODO General: Example usage of FormTester
@author Ingram Chen
@author Juergen Donnerstag
@author Frank Bille
@since 1.2.6