I'm trying to implement a link with dynamic text in Wicket, with the username as its text. My first thought was to do something like this in the markup:
<a wicket:id="somelink"><wicket:message key="some.key">bla bla</wicket:message></a>
With the properties file looking like this:
some.key=Username is: {0}
And the code:
String username = ...
add(new Link("somelink", new StringResourceModel("some.key", this, null, username)) {
...
});
The problem is that I have no idea how to test that the link's text is being set to the username (in the unit test that is).
I've tried:
WicketTester
's assertLabel
method, but it can't cast the Link
to a Label
Link
's getModelObject()
method returns the original message (i.e. before formatting the username into it)wicket:message
element ?)Any thoughts ?
How about
WicketTester.getTagById(java.lang.String)
or WicketTester.getTagByWicketId(java.lang.String)
? These return a TagTester
Object and TagTester.getValue()
returns the value for this tag. This includes all data between the open tag and the close tag as a String. Then you can use assertEquals on the resulting String and your expectation...
TagTester link = WicketTester.getTagByWicketId("someLink");
assertNotNull(link);
String linkText = link.getValue();
asserEquals("Username is: " + username, linkText);