java.lang.Object
org.apache.wicket.util.tester.BaseWicketTester
org.apache.wicket.util.tester.WicketTester
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
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.wicket.util.tester.BaseWicketTester
BaseWicketTester.StartComponentInPage, BaseWicketTester.TestFilterConfig
-
Constructor Summary
ConstructorDescriptionCreates aWicketTester
and automatically creates aWebApplication
, but the tester will have no home page.WicketTester
(Class<? extends Page> homePage) Creates aWicketTester
and automatically creates aWebApplication
.WicketTester
(WebApplication application) Creates aWicketTester
.WicketTester
(WebApplication application, boolean init) Creates aWicketTester
to help unit testing.WicketTester
(WebApplication application, jakarta.servlet.ServletContext servletCtx) Creates aWicketTester
to help unit testing.WicketTester
(WebApplication application, jakarta.servlet.ServletContext servletCtx, boolean init) Creates aWicketTester
to help unit testing.WicketTester
(WebApplication application, String path) Creates aWicketTester
to help unit testing. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Asserts that the Ajax location header is present.void
assertBehavior
(String path, Class<? extends Behavior> expectedBehaviorClass) Asserts that theComponent
a the given path has a behavior of the given type.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.void
assertComponent
(String path, Class<? extends Component> expectedComponentClass) Asserts aComponent
class.void
assertComponentFeedbackMessage
(Component component, String key, IModel<?> model, IFeedbackMessageFilter filter) Asserts that there is a feedback message provided by a given componentvoid
assertComponentOnAjaxResponse
(String componentPath) Tests that aComponent
has been added to aAjaxRequestTarget
, usingIPartialPageRequestHandler.add(Component...)
.void
assertComponentOnAjaxResponse
(Component component) Tests that aComponent
has been added to aAjaxRequestTarget
, usingIPartialPageRequestHandler.add(Component...)
.void
assertContains
(String pattern) Asserts the content of last rendered page contains (matches) a given regex pattern.void
assertContainsNot
(String pattern) The opposite ofassertContains(String)
.void
assertDisabled
(String path) assert component is enabled.void
assertEnabled
(String path) assert component is enabled.void
assertErrorMessages
(Serializable... expectedErrorMessages) Asserts error-level feedback messages.void
assertFeedback
(String path, Serializable... messages) Assert that a particular feedback panel is rendering certain messages.void
assertFeedbackMessages
(IFeedbackMessageFilter filter, Serializable... expectedMessages) Assert there are feedback messages accepted by the provided filter.void
assertInfoMessages
(Serializable... expectedInfoMessages) Assert info-level feedback messages.void
assertInvisible
(String path) Asserts that aComponent
is invisible.void
assertLabel
(String path, String expectedLabelText) Asserts the text of aLabel
Component
.void
assertMarkupLocale
(Component component, Locale expectedLocale) Asserts that a component's markup has loaded with the given localevoid
assertMarkupStyle
(Component component, String expectedStyle) Asserts that a component's markup has loaded with the given style.void
assertMarkupVariation
(Component component, String expectedVariation) Asserts that a component's markup has loaded with the given variationvoid
assertModelValue
(String path, Object expectedValue) Asserts the model value of a component.void
Asserts no error-level feedback messages.void
assertNoFeedbackMessage
(int level) Asserts there are no feedback messages with a certain level.void
Asserts no info-level feedback messages.void
assertNotRequired
(String path) assert form component is required.void
assertRedirectUrl
(String expectedRedirectUrl) Assert that the last request redirected to the given Url.void
assertRenderedPage
(Class<? extends Page> expectedRenderedPageClass) Asserts a last-renderedPage
class.void
assertRequired
(String path) assert form component is required.void
assertResultPage
(Class<?> clazz, String filename) Asserts last-renderedPage
against an expected HTML document.void
assertResultPage
(String expectedDocument) Asserts last-renderedPage
against an expected HTML document as aString
void
assertUsability
(Component component) Checks whether a component is visible and/or enabled before usagevoid
assertVisible
(String path) Asserts that aComponent
is visible.void
void
executeBehavior
(Class<?> testClass, AbstractAjaxBehavior behavior, String filename) void
executeListener
(Class<?> testClass, Component component, String filename) <T extends Page>
voidexecuteTest
(Class<?> testClass, Class<T> pageClass, String filename) Use-Dwicket.replace.expected.results=true
to automatically replace the expected output file.<T extends Page>
voidexecuteTest
(Class<?> testClass, Class<T> pageClass, PageParameters parameters, String filename) Use-Dwicket.replace.expected.results=true
to automatically replace the expected output file.void
executeTest
(Class<?> testClass, Component component, String filename) Use-Dwicket.replace.expected.results=true
to automatically replace the expected output file.void
executeTest
(Class<?> testClass, Page page, String filename) Use-Dwicket.replace.expected.results=true
to automatically replace the expected output file.static String
Returns the current Maven build directory taken from the basedir system property, or null if not setMethods inherited from class org.apache.wicket.util.tester.BaseWicketTester
addRequestHeader, applyRequest, assertExists, assertNotExists, checkUsability, cleanupFeedbackMessages, cleanupFeedbackMessages, clearFeedbackMessages, clickLink, clickLink, createOriginHeader, createPage, createPageMarkup, debugComponentTrees, debugComponentTrees, destroy, dumpPage, executeAjaxEvent, executeAjaxEvent, executeAjaxUrl, executeAllTimerBehaviors, executeBehavior, executeListener, executeUrl, getAllComponentsByWicketId, getApplication, getComponentFromLastRenderedPage, getComponentFromLastRenderedPage, getComponentFromLastRenderedPage, getContentDispositionFromResponseHeader, getContentLengthFromResponseHeader, getContentTypeFromResponseHeader, getFeedbackMessages, getFirstComponentByWicketId, getHttpSession, getLastModifiedFromResponseHeader, getLastRenderedPage, getLastRequest, getLastResponse, getLastResponseAsString, getMessages, getPreviousRequests, getPreviousResponses, getRequest, getRequestCycle, getResourcePollFrequency, getResponse, getServletContext, getSession, getTagById, getTagByWicketId, getTagsByWicketId, getWicketAjaxBaseUrlEncodedInLastResponse, hasLabel, hasNoErrorMessage, hasNoFeedbackMessage, hasNoInfoMessage, ifContains, ifContainsNot, invokeListener, invokeListener, isComponent, isComponentOnAjaxResponse, isDisabled, isEnabled, isEqual, isExposeExceptions, isFollowRedirects, isInvisible, isNotRequired, isNotRequired, isRenderedPage, isRequired, isRequired, isResultPage, isUseRequestUrlAsBase, isVisible, newFormTester, newFormTester, newServletWebResponse, newTestPageManagerProvider, processRequest, processRequest, processRequest, processRequest, processRequest, servletRequestLocale, setExposeExceptions, setFollowRedirects, setLastResponse, setRequest, setUseRequestUrlAsBase, startComponentInPage, startComponentInPage, startComponentInPage, startComponentInPage, startPage, startPage, startPage, startPage, startResource, startResourceReference, startResourceReference, submitForm, submitForm, urlFor, urlFor, urlFor
-
Constructor Details
-
WicketTester
public WicketTester()Creates aWicketTester
and automatically creates aWebApplication
, but the tester will have no home page. -
WicketTester
Creates aWicketTester
and automatically creates aWebApplication
.- Parameters:
homePage
- a home pageClass
-
WicketTester
Creates aWicketTester
.- Parameters:
application
- aWicketTester
WebApplication
object
-
WicketTester
Creates aWicketTester
to help unit testing.- Parameters:
application
- aWicketTester
WebApplication
objectpath
- the absolute path on disk to the web application's contents (e.g. war root) - may benull
- See Also:
-
WicketTester
Creates aWicketTester
to help unit testing.- Parameters:
application
- aWicketTester
WebApplication
objectservletCtx
- the servlet context used as backend
-
WicketTester
Creates aWicketTester
to help unit testing.- Parameters:
application
- aWicketTester
WebApplication
objectinit
- force the application to be initialized (default = true)
-
WicketTester
public WicketTester(WebApplication application, jakarta.servlet.ServletContext servletCtx, boolean init) Creates aWicketTester
to help unit testing.- Parameters:
application
- aWicketTester
WebApplication
objectservletCtx
- the servlet context used as backendinit
- force the application to be initialized (default = true)
-
-
Method Details
-
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
Asserts that the Ajax location header is present. -
assertComponent
Asserts aComponent
class.- Parameters:
path
- path toComponent
expectedComponentClass
- expectedComponent
class
-
assertBehavior
Asserts that theComponent
a the given path has a behavior of the given type.- Parameters:
path
- path toComponent
expectedBehaviorClass
- expectedBehavior
class
-
assertComponentOnAjaxResponse
Tests that aComponent
has been added to aAjaxRequestTarget
, usingIPartialPageRequestHandler.add(Component...)
. This method actually tests that aComponent
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
- aComponent
to be tested
-
assertComponentOnAjaxResponse
Tests that aComponent
has been added to aAjaxRequestTarget
, usingIPartialPageRequestHandler.add(Component...)
. This method actually tests that aComponent
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
- aComponent
path to test
-
assertContains
Asserts the content of last rendered page contains (matches) a given regex pattern.- Parameters:
pattern
- a regex pattern to match
-
assertContainsNot
The opposite ofassertContains(String)
.- Parameters:
pattern
- pattern
-
assertMarkupVariation
Asserts that a component's markup has loaded with the given variation- Parameters:
component
- The component which markup to checkexpectedVariation
- The expected variation of the component's markup
-
assertMarkupStyle
Asserts that a component's markup has loaded with the given style.- Parameters:
component
- The component which markup to checkexpectedStyle
- The expected style of the component's markup. For example: green inMyPanel_green.html
-
assertMarkupLocale
Asserts that a component's markup has loaded with the given locale- Parameters:
component
- The component which markup to checkexpectedLocale
- The expected locale of the component's markup
-
assertErrorMessages
Asserts error-level feedback messages.- Parameters:
expectedErrorMessages
- expected error messages
-
assertInfoMessages
Assert info-level feedback messages.- Parameters:
expectedInfoMessages
- expected info messages
-
assertFeedbackMessages
Assert there are feedback messages accepted by the provided filter.- Parameters:
filter
- the filter that will decide which messages to checkexpectedMessages
- 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
Assert that a particular feedback panel is rendering certain messages. NOTE: this casts the component at the specified path to aFeedbackPanel
, so it will not work with customIFeedback
implementations unless you are subclassingFeedbackPanel
- Parameters:
path
- path to the feedback panelmessages
- messages expected to be rendered
-
assertInvisible
Asserts that aComponent
is invisible.- Parameters:
path
- path toComponent
-
assertLabel
Asserts the text of aLabel
Component
.- Parameters:
path
- path toLabel
Component
expectedLabelText
- expected text of theLabel
-
assertModelValue
Asserts the model value of a component.- Parameters:
path
- path to the component on the pageexpectedValue
- expected value of the component's model
-
assertNoErrorMessage
Asserts no error-level feedback messages. -
assertNoInfoMessage
Asserts no info-level feedback messages. -
assertNoFeedbackMessage
Asserts there are no feedback messages with a certain level.- Parameters:
level
- the level to check for
-
assertRenderedPage
Asserts a last-renderedPage
class.- Parameters:
expectedRenderedPageClass
- expected class of last renderedPage
-
assertResultPage
Asserts last-renderedPage
against an expected HTML document.Use
-Dwicket.replace.expected.results=true
to automatically replace the expected output file.- Overrides:
assertResultPage
in classBaseWicketTester
- Parameters:
clazz
-Class
used to load the file (relative toclazz
package)filename
- expected output filenameString
- Throws:
Exception
-
assertResultPage
Asserts last-renderedPage
against an expected HTML document as aString
- Parameters:
expectedDocument
- expected outputString
-
assertVisible
Asserts that aComponent
is visible.- Parameters:
path
- path to aComponent
-
assertEnabled
assert component is enabled.- Parameters:
path
- path to component
-
assertDisabled
assert component is enabled.- Parameters:
path
- path to component
-
assertRequired
assert form component is required.- Parameters:
path
- path to form component
-
assertNotRequired
assert form component is required.- Parameters:
path
- path to form component
-
assertUsability
Checks whether a component is visible and/or enabled before usage- Parameters:
component
-
-
clickLink
- 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
Use-Dwicket.replace.expected.results=true
to automatically replace the expected output file.- Parameters:
testClass
-page
-filename
-- Throws:
Exception
-
executeTest
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
Assert that the last request redirected to the given Url.- Parameters:
expectedRedirectUrl
- expected
-