org.apache.wicket.util.tester
Class WicketTester

java.lang.Object
  extended by org.apache.wicket.protocol.http.MockWebApplication
      extended by org.apache.wicket.util.tester.BaseWicketTester
          extended by org.apache.wicket.util.tester.WicketTester

public class WicketTester
extends BaseWicketTester

A helper class to ease unit testing of Wicket applications without the need for a servlet container. To start a test, either use startPage or startPanel:

 // production 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 ITestPageSource to provide testing page instance for 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

Since:
1.2.6
Author:
Ingram Chen, Juergen Donnerstag, Frank Bille

Nested Class Summary
static class WicketTester.DummyWebApplication
          Default dummy web application for testing.
static class WicketTester.NonPageCachingDummyWebApplication
          Dummy web application that does not support back button support but is cheaper to use for unit tests.
 
Constructor Summary
WicketTester()
          Creates a WicketTester and automatically creates a WebApplication, but the tester will have no home page.
WicketTester(java.lang.Class homePage)
          Creates a WicketTester and automatically creates a WebApplication.
WicketTester(WebApplication application)
          Creates a WicketTester.
WicketTester(WebApplication application, java.lang.String path)
          Creates a WicketTester to help unit testing.
 
Method Summary
 void assertAjaxLocation()
          Asserts that the Ajax location header is present.
 void assertComponent(java.lang.String path, java.lang.Class expectedComponentClass)
          Asserts a Component class.
 void assertComponentOnAjaxResponse(Component component)
          Tests that a Component has been added to a AjaxRequestTarget, using AjaxRequestTarget.addComponent(Component).
 void assertComponentOnAjaxResponse(java.lang.String componentPath)
          Tests that a Component has been added to a AjaxRequestTarget, using AjaxRequestTarget.addComponent(Component).
 void assertContains(java.lang.String pattern)
          Asserts the content of last rendered page contains (matches) a given regex pattern.
 void assertErrorMessages(java.lang.String[] expectedErrorMessages)
          Asserts error-level feedback messages.
 void assertInfoMessages(java.lang.String[] expectedInfoMessages)
          Assert info-level feedback messages.
 void assertInvisible(java.lang.String path)
          Asserts that a Component is invisible.
 void assertLabel(java.lang.String path, java.lang.String expectedLabelText)
          Asserts the text of a Label Component.
 void assertListView(java.lang.String path, java.util.List expectedList)
          Asserts the model of a ListView.
 void assertModelValue(java.lang.String path, java.lang.Object expectedValue)
          Asserts the model value of a component.
 void assertNoErrorMessage()
          Asserts no error-level feedback messages.
 void assertNoInfoMessage()
          Asserts no info-level feedback messages.
 void assertPageLink(java.lang.String path, java.lang.Class expectedPageClass)
          Asserts a PageLink link to a Page class.
 void assertRenderedPage(java.lang.Class expectedRenderedPageClass)
          Asserts a last-rendered Page class.
 void assertResultPage(java.lang.Class clazz, java.lang.String filename)
          Asserts last-rendered Page against an expected HTML document.
 void assertResultPage(java.lang.String expectedDocument)
          Asserts last-rendered Page against an expected HTML document as a String
 void assertVisible(java.lang.String path)
          Asserts that a Component is visible.
 
Methods inherited from class org.apache.wicket.util.tester.BaseWicketTester
callOnBeginRequest, clickLink, clickLink, debugComponentTrees, debugComponentTrees, dumpPage, executeAjaxEvent, executeAjaxEvent, executeBehavior, executeListener, getComponentFromLastRenderedPage, getContentDispositionFromResponseHeader, getContentLengthFromResponseHeader, getContentTypeFromResponseHeader, getLastModifiedFromResponseHeader, getMessages, getTagById, getTagByWicketId, hasLabel, hasNoErrorMessage, hasNoInfoMessage, ifContains, isComponent, isComponentOnAjaxResponse, isInvisible, isPageLink, isRenderedPage, isResultPage, isVisible, newFormTester, newFormTester, resolveRequestCycle, setParameterForNextRequest, startComponent, startPage, startPage, startPage, startPage, startPanel, startPanel, submitForm
 
Methods inherited from class org.apache.wicket.protocol.http.MockWebApplication
createRequestCycle, destroy, getApplication, getLastRenderedPage, getParametersForNextRequest, getPreviousRenderedPage, getServletRequest, getServletResponse, getServletSession, getWicketRequest, getWicketResponse, getWicketSession, initializeHttpSessionAsTemporary, newServletContext, postProcessRequestCycle, processRequestCycle, processRequestCycle, processRequestCycle, processRequestCycle, processRequestCycle, setParametersForNextRequest, setupRequestAndResponse, setupRequestAndResponse
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WicketTester

public WicketTester()
Creates a WicketTester and automatically creates a WebApplication, but the tester will have no home page.


WicketTester

public WicketTester(java.lang.Class homePage)
Creates a WicketTester and automatically creates a WebApplication.

Parameters:
homePage - a home page Class

WicketTester

public WicketTester(WebApplication application)
Creates a WicketTester.

Parameters:
application - a WicketTester WebApplication object

WicketTester

public WicketTester(WebApplication application,
                    java.lang.String path)
Creates a WicketTester to help unit testing.

Parameters:
application - a WicketTester WebApplication object
path - the absolute path on disk to the web application's contents (e.g. war root) - may be null
See Also:
MockWebApplication.MockWebApplication( org.apache.wicket.protocol.http.WebApplication, String)
Method Detail

assertAjaxLocation

public void assertAjaxLocation()
Asserts that the Ajax location header is present.


assertComponent

public void assertComponent(java.lang.String path,
                            java.lang.Class expectedComponentClass)
Asserts a Component class.

Parameters:
path - path to Component
expectedComponentClass - expected Component class

assertComponentOnAjaxResponse

public void assertComponentOnAjaxResponse(Component component)
Tests that a Component has been added to a AjaxRequestTarget, using AjaxRequestTarget.addComponent(Component). This method actually tests that a Component is on the Ajax response sent back to the client.

PLEASE NOTE! This method doesn't actually insert the Component in the client DOM tree, using Javascript. But it shouldn't be needed because you just have to trust that Wicket Ajax Javascript works.

Parameters:
component - a Component to be tested

assertComponentOnAjaxResponse

public void assertComponentOnAjaxResponse(java.lang.String componentPath)
Tests that a Component has been added to a AjaxRequestTarget, using AjaxRequestTarget.addComponent(Component). This method actually tests that a Component is on the Ajax response sent back to the client.

PLEASE NOTE! This method doesn't actually insert the Component in the client DOM tree, using Javascript. But it shouldn't be needed because you just have to trust that Wicket Ajax Javascript works.

Parameters:
componentPath - a Component path to test

assertContains

public void assertContains(java.lang.String pattern)
Asserts the content of last rendered page contains (matches) a given regex pattern.

Parameters:
pattern - a reqex pattern to match

assertErrorMessages

public void assertErrorMessages(java.lang.String[] expectedErrorMessages)
Asserts error-level feedback messages.

Parameters:
expectedErrorMessages - expected error messages

assertInfoMessages

public void assertInfoMessages(java.lang.String[] expectedInfoMessages)
Assert info-level feedback messages.

Parameters:
expectedInfoMessages - expected info messages

assertInvisible

public void assertInvisible(java.lang.String path)
Asserts that a Component is invisible.

Parameters:
path - path to Component

assertLabel

public void assertLabel(java.lang.String path,
                        java.lang.String expectedLabelText)
Asserts the text of a Label Component.

Parameters:
path - path to Label Component
expectedLabelText - expected text of the Label

assertModelValue

public void assertModelValue(java.lang.String path,
                             java.lang.Object expectedValue)
Asserts the model value of a component.

Parameters:
path - path to the component on the page
expectedValue - expected value of the component's model

assertListView

public void assertListView(java.lang.String path,
                           java.util.List expectedList)
Asserts the model of a ListView.

Overrides:
assertListView in class BaseWicketTester
Parameters:
path - path to a ListView Component
expectedList - expected List in the model of the given ListView

assertNoErrorMessage

public void assertNoErrorMessage()
Asserts no error-level feedback messages.


assertNoInfoMessage

public void assertNoInfoMessage()
Asserts no info-level feedback messages.


assertPageLink

public void assertPageLink(java.lang.String path,
                           java.lang.Class expectedPageClass)
Asserts a PageLink link to a Page class.

Parameters:
path - path to PageLink Component
expectedPageClass - expected Page class to link

assertRenderedPage

public void assertRenderedPage(java.lang.Class expectedRenderedPageClass)
Asserts a last-rendered Page class.

Parameters:
expectedRenderedPageClass - expected class of last rendered Page

assertResultPage

public void assertResultPage(java.lang.Class clazz,
                             java.lang.String filename)
                      throws java.lang.Exception
Asserts last-rendered Page against an expected HTML document.

Use -Dwicket.replace.expected.results=true to automatically replace the expected output file.

Overrides:
assertResultPage in class BaseWicketTester
Parameters:
clazz - Class used to load the file (relative to clazz package)
filename - expected output filename String
Throws:
java.lang.Exception

assertResultPage

public void assertResultPage(java.lang.String expectedDocument)
                      throws java.lang.Exception
Asserts last-rendered Page against an expected HTML document as a String

Parameters:
expectedDocument - expected output String
Throws:
java.lang.Exception

assertVisible

public void assertVisible(java.lang.String path)
Asserts that a Component is visible.

Parameters:
path - path to a Component


Copyright © 2004-2009 Apache Software Foundation. All Rights Reserved.