Class WicketTester

java.lang.Object
org.apache.wicket.util.tester.BaseWicketTester
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 YourPage("mock message"));
        tester.assertRenderedPage(YourPage.class);
        tester.assertLabel("yourMessage", "mock message");
        // assert feedback messages in INFO Level
        tester.assertInfoMessages(new String[] { "Wicket Rocks ;-)" });
 }
 
Many methods require a 'path' parameter. E.g. the page relative path can be obtained via Component.getPageRelativePath(). Since each Component has an ID/name, any Component can also be referenced by its ID MarkupContainer.get(String). And since MarkupContainer's and its subclasses are containers which allow to add Components (in sync with the markup hierarchy), you may not only access direct children but also grandchildren like get("myPanel:myForm:myNameField") separating each ID with a ':'.

Cookie handling

There are some expectations about wicket tester cookie handling which should match as best as it can be with a real client server request response cycle:
  • all valid cookies set before a request is made (tester.getRequest().addCookie()) should appear in the page request
  • all cookies set in the response should appear in the last response (tester.getLastResponse()) after the request is made (expired cookies and others)
  • all cookies set in the response should appear even after a redirect response is made until the final response (tester.getLastResponse()) is written to the client (wicket tester)
  • all valid cookies (maxAge!=0) from the last response should be added to the next request cookies (tester.getRequest().getCookies())
Since:
1.2.6
Author:
Ingram Chen, Juergen Donnerstag, Frank Bille
  • Constructor Details

    • WicketTester

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

      public WicketTester(Class<? extends Page> 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, 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:
    • WicketTester

      public WicketTester(WebApplication application, jakarta.servlet.ServletContext servletCtx)
      Creates a WicketTester to help unit testing.
      Parameters:
      application - a WicketTester WebApplication object
      servletCtx - the servlet context used as backend
    • WicketTester

      public WicketTester(WebApplication application, boolean init)
      Creates a WicketTester to help unit testing.
      Parameters:
      application - a WicketTester WebApplication object
      init - force the application to be initialized (default = true)
    • WicketTester

      public WicketTester(WebApplication application, jakarta.servlet.ServletContext servletCtx, boolean init)
      Creates a WicketTester to help unit testing.
      Parameters:
      application - a WicketTester WebApplication object
      servletCtx - the servlet context used as backend
      init - force the application to be initialized (default = true)
  • Method Details

    • getBasedir

      public static String getBasedir()
      Returns the current Maven build directory taken from the basedir system property, or null if not set
      Returns:
      path with a trailing slash
    • assertAjaxLocation

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

      public void assertComponent(String path, Class<? extends Component> expectedComponentClass)
      Asserts a Component class.
      Parameters:
      path - path to Component
      expectedComponentClass - expected Component class
    • assertBehavior

      public void assertBehavior(String path, Class<? extends Behavior> expectedBehaviorClass)
      Asserts that the Component a the given path has a behavior of the given type.
      Parameters:
      path - path to Component
      expectedBehaviorClass - expected Behavior class
    • assertComponentOnAjaxResponse

      public void assertComponentOnAjaxResponse(Component component)
      Tests that a Component has been added to a AjaxRequestTarget, using IPartialPageRequestHandler.add(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(String componentPath)
      Tests that a Component has been added to a AjaxRequestTarget, using IPartialPageRequestHandler.add(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(String pattern)
      Asserts the content of last rendered page contains (matches) a given regex pattern.
      Parameters:
      pattern - a regex pattern to match
    • assertContainsNot

      public void assertContainsNot(String pattern)
      The opposite of assertContains(String).
      Parameters:
      pattern - pattern
    • assertMarkupVariation

      public void assertMarkupVariation(Component component, String expectedVariation)
      Asserts that a component's markup has loaded with the given variation
      Parameters:
      component - The component which markup to check
      expectedVariation - The expected variation of the component's markup
    • assertMarkupStyle

      public void assertMarkupStyle(Component component, String expectedStyle)
      Asserts that a component's markup has loaded with the given style.
      Parameters:
      component - The component which markup to check
      expectedStyle - The expected style of the component's markup. For example: green in MyPanel_green.html
    • assertMarkupLocale

      public void assertMarkupLocale(Component component, Locale expectedLocale)
      Asserts that a component's markup has loaded with the given locale
      Parameters:
      component - The component which markup to check
      expectedLocale - The expected locale of the component's markup
    • assertErrorMessages

      public void assertErrorMessages(Serializable... expectedErrorMessages)
      Asserts error-level feedback messages.
      Parameters:
      expectedErrorMessages - expected error messages
    • assertInfoMessages

      public void assertInfoMessages(Serializable... expectedInfoMessages)
      Assert info-level feedback messages.
      Parameters:
      expectedInfoMessages - expected info messages
    • assertFeedbackMessages

      public void assertFeedbackMessages(IFeedbackMessageFilter filter, Serializable... expectedMessages)
      Assert there are feedback messages accepted by the provided filter.
      Parameters:
      filter - the filter that will decide which messages to check
      expectedMessages - expected feedback messages
    • assertComponentFeedbackMessage

      public void assertComponentFeedbackMessage(Component component, String key, IModel<?> model, IFeedbackMessageFilter filter)
      Asserts that there is a feedback message provided by a given component
      Parameters:
      component - the component that provided the expected feedback message. Optional.
      key - the resource key for the feedback message. Mandatory.
      model - the model used for interpolating the feedback message. Optional.
      filter - the filter that decides in which messages to look in. E.g. with a specific level, rendered or not, etc.
    • assertFeedback

      public void assertFeedback(String path, Serializable... messages)
      Assert that a particular feedback panel is rendering certain messages. NOTE: this casts the component at the specified path to a FeedbackPanel, so it will not work with custom IFeedback implementations unless you are subclassing FeedbackPanel
      Parameters:
      path - path to the feedback panel
      messages - messages expected to be rendered
    • assertInvisible

      public void assertInvisible(String path)
      Asserts that a Component is invisible.
      Parameters:
      path - path to Component
    • assertLabel

      public void assertLabel(String path, 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(String path, 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
    • assertNoErrorMessage

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

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

      public void assertNoFeedbackMessage(int level)
      Asserts there are no feedback messages with a certain level.
      Parameters:
      level - the level to check for
    • assertRenderedPage

      public void assertRenderedPage(Class<? extends Page> expectedRenderedPageClass)
      Asserts a last-rendered Page class.
      Parameters:
      expectedRenderedPageClass - expected class of last rendered Page
    • assertResultPage

      public void assertResultPage(Class<?> clazz, String filename) throws 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:
      Exception
    • assertResultPage

      public void assertResultPage(String expectedDocument)
      Asserts last-rendered Page against an expected HTML document as a String
      Parameters:
      expectedDocument - expected output String
    • assertVisible

      public void assertVisible(String path)
      Asserts that a Component is visible.
      Parameters:
      path - path to a Component
    • assertEnabled

      public void assertEnabled(String path)
      assert component is enabled.
      Parameters:
      path - path to component
    • assertDisabled

      public void assertDisabled(String path)
      assert component is enabled.
      Parameters:
      path - path to component
    • assertRequired

      public void assertRequired(String path)
      assert form component is required.
      Parameters:
      path - path to form component
    • assertNotRequired

      public void assertNotRequired(String path)
      assert form component is required.
      Parameters:
      path - path to form component
    • assertUsability

      public void assertUsability(Component component)
      Checks whether a component is visible and/or enabled before usage
      Parameters:
      component -
    • clickLink

      public void clickLink(Component link)
      Parameters:
      link -
    • assertBookmarkablePageLink

      public void assertBookmarkablePageLink(String id, Class<? extends WebPage> pageClass, PageParameters parameters)
      Asserts that that the BookmarkablePageLink identified by "id" points to the page as expected - including parameters.
      Parameters:
      id -
      pageClass -
      parameters -
    • executeTest

      public <T extends Page> void executeTest(Class<?> testClass, Class<T> pageClass, String filename) throws Exception
      Use -Dwicket.replace.expected.results=true to automatically replace the expected output file.
      Type Parameters:
      T -
      Parameters:
      testClass -
      pageClass -
      filename -
      Throws:
      Exception
    • executeTest

      public void executeTest(Class<?> testClass, Page page, String filename) throws Exception
      Use -Dwicket.replace.expected.results=true to automatically replace the expected output file.
      Parameters:
      testClass -
      page -
      filename -
      Throws:
      Exception
    • executeTest

      public void executeTest(Class<?> testClass, Component component, String filename) throws Exception
      Use -Dwicket.replace.expected.results=true to automatically replace the expected output file.
      Parameters:
      testClass -
      component -
      filename -
      Throws:
      Exception
    • executeTest

      public <T extends Page> void executeTest(Class<?> testClass, Class<T> pageClass, PageParameters parameters, String filename) throws Exception
      Use -Dwicket.replace.expected.results=true to automatically replace the expected output file.
      Type Parameters:
      T -
      Parameters:
      testClass -
      pageClass -
      parameters -
      filename -
      Throws:
      Exception
    • executeListener

      public void executeListener(Class<?> testClass, Component component, String filename) throws Exception
      Parameters:
      testClass -
      component -
      filename -
      Throws:
      Exception
    • executeBehavior

      public void executeBehavior(Class<?> testClass, AbstractAjaxBehavior behavior, String filename) throws Exception
      Parameters:
      testClass -
      behavior -
      filename -
      Throws:
      Exception
    • assertRedirectUrl

      public void assertRedirectUrl(String expectedRedirectUrl)
      Assert that the last request redirected to the given Url.
      Parameters:
      expectedRedirectUrl - expected