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
ConstructorsConstructorDescriptionCreates aWicketTesterand automatically creates aWebApplication, but the tester will have no home page.WicketTester(Class<? extends Page> homePage) Creates aWicketTesterand automatically creates aWebApplication.WicketTester(WebApplication application) Creates aWicketTester.WicketTester(WebApplication application, boolean init) Creates aWicketTesterto help unit testing.WicketTester(WebApplication application, jakarta.servlet.ServletContext servletCtx) Creates aWicketTesterto help unit testing.WicketTester(WebApplication application, jakarta.servlet.ServletContext servletCtx, boolean init) Creates aWicketTesterto help unit testing.WicketTester(WebApplication application, String path) Creates aWicketTesterto help unit testing. -
Method Summary
Modifier and TypeMethodDescriptionvoidAsserts that the Ajax location header is present.voidassertBehavior(String path, Class<? extends Behavior> expectedBehaviorClass) Asserts that theComponenta the given path has a behavior of the given type.voidassertBookmarkablePageLink(String id, Class<? extends WebPage> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters) Asserts that that the BookmarkablePageLink identified by "id" points to the page as expected - including parameters.voidassertComponent(String path, Class<? extends Component> expectedComponentClass) Asserts aComponentclass.voidassertComponentFeedbackMessage(Component component, String key, IModel<?> model, IFeedbackMessageFilter filter) Asserts that there is a feedback message provided by a given componentvoidassertComponentOnAjaxResponse(String componentPath) Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...).voidassertComponentOnAjaxResponse(Component component) Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...).voidassertContains(String pattern) Asserts the content of last rendered page contains (matches) a given regex pattern.voidassertContainsNot(String pattern) The opposite ofassertContains(String).voidassertDisabled(String path) assert component is enabled.voidassertEnabled(String path) assert component is enabled.voidassertErrorMessages(Serializable... expectedErrorMessages) Asserts error-level feedback messages.voidassertFeedback(String path, Serializable... messages) Assert that a particular feedback panel is rendering certain messages.voidassertFeedbackMessages(IFeedbackMessageFilter filter, Serializable... expectedMessages) Assert there are feedback messages accepted by the provided filter.voidassertInfoMessages(Serializable... expectedInfoMessages) Assert info-level feedback messages.voidassertInvisible(String path) Asserts that aComponentis invisible.voidassertLabel(String path, String expectedLabelText) Asserts the text of aLabelComponent.voidassertMarkupLocale(Component component, Locale expectedLocale) Asserts that a component's markup has loaded with the given localevoidassertMarkupStyle(Component component, String expectedStyle) Asserts that a component's markup has loaded with the given style.voidassertMarkupVariation(Component component, String expectedVariation) Asserts that a component's markup has loaded with the given variationvoidassertModelValue(String path, Object expectedValue) Asserts the model value of a component.voidAsserts no error-level feedback messages.voidassertNoFeedbackMessage(int level) Asserts there are no feedback messages with a certain level.voidAsserts no info-level feedback messages.voidassertNotRequired(String path) assert form component is required.voidassertRedirectUrl(String expectedRedirectUrl) Assert that the last request redirected to the given Url.voidassertRenderedPage(Class<? extends Page> expectedRenderedPageClass) Asserts a last-renderedPageclass.voidassertRequired(String path) assert form component is required.voidassertResultPage(Class<?> clazz, String filename) Asserts last-renderedPageagainst an expected HTML document.voidassertResultPage(String expectedDocument) Asserts last-renderedPageagainst an expected HTML document as aStringvoidassertUsability(Component component) Checks whether a component is visible and/or enabled before usagevoidassertVisible(String path) Asserts that aComponentis visible.voidvoidexecuteBehavior(Class<?> testClass, AbstractAjaxBehavior behavior, String filename) voidexecuteListener(Class<?> testClass, Component component, String filename) <T extends Page>
voidexecuteTest(Class<?> testClass, Class<T> pageClass, String filename) Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.<T extends Page>
voidexecuteTest(Class<?> testClass, Class<T> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters, String filename) Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.voidexecuteTest(Class<?> testClass, Component component, String filename) Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.voidexecuteTest(Class<?> testClass, Page page, String filename) Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.static StringReturns 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 aWicketTesterand automatically creates aWebApplication, but the tester will have no home page. -
WicketTester
Creates aWicketTesterand automatically creates aWebApplication.- Parameters:
homePage- a home pageClass
-
WicketTester
Creates aWicketTester.- Parameters:
application- aWicketTesterWebApplicationobject
-
WicketTester
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectpath- the absolute path on disk to the web application's contents (e.g. war root) - may benull- See Also:
-
WicketTester
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectservletCtx- the servlet context used as backend
-
WicketTester
Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectinit- force the application to be initialized (default = true)
-
WicketTester
public WicketTester(WebApplication application, jakarta.servlet.ServletContext servletCtx, boolean init) Creates aWicketTesterto help unit testing.- Parameters:
application- aWicketTesterWebApplicationobjectservletCtx- 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 aComponentclass.- Parameters:
path- path toComponentexpectedComponentClass- expectedComponentclass
-
assertBehavior
Asserts that theComponenta the given path has a behavior of the given type.- Parameters:
path- path toComponentexpectedBehaviorClass- expectedBehaviorclass
-
assertComponentOnAjaxResponse
Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...). This method actually tests that aComponentis on the Ajax response sent back to the client.PLEASE NOTE! This method doesn't actually insert the
Componentin 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- aComponentto be tested
-
assertComponentOnAjaxResponse
Tests that aComponenthas been added to aAjaxRequestTarget, usingIPartialPageRequestHandler.add(Component...). This method actually tests that aComponentis on the Ajax response sent back to the client.PLEASE NOTE! This method doesn't actually insert the
Componentin 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- aComponentpath 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 customIFeedbackimplementations unless you are subclassingFeedbackPanel- Parameters:
path- path to the feedback panelmessages- messages expected to be rendered
-
assertInvisible
Asserts that aComponentis invisible.- Parameters:
path- path toComponent
-
assertLabel
Asserts the text of aLabelComponent.- Parameters:
path- path toLabelComponentexpectedLabelText- 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-renderedPageclass.- Parameters:
expectedRenderedPageClass- expected class of last renderedPage
-
assertResultPage
Asserts last-renderedPageagainst an expected HTML document.Use
-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Overrides:
assertResultPagein classBaseWicketTester- Parameters:
clazz-Classused to load the file (relative toclazzpackage)filename- expected output filenameString- Throws:
Exception
-
assertResultPage
Asserts last-renderedPageagainst an expected HTML document as aString- Parameters:
expectedDocument- expected outputString
-
assertVisible
Asserts that aComponentis 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, org.apache.wicket.request.mapper.parameter.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=trueto automatically replace the expected output file.- Type Parameters:
T-- Parameters:
testClass-pageClass-filename-- Throws:
Exception
-
executeTest
Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Parameters:
testClass-page-filename-- Throws:
Exception
-
executeTest
Use-Dwicket.replace.expected.results=trueto automatically replace the expected output file.- Parameters:
testClass-component-filename-- Throws:
Exception
-
executeTest
public <T extends Page> void executeTest(Class<?> testClass, Class<T> pageClass, org.apache.wicket.request.mapper.parameter.PageParameters parameters, String filename) throws Exception Use-Dwicket.replace.expected.results=trueto 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
-