javaunit-testingtddwicketwicket-tester

How to test a link's text in Wicket


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:

Any thoughts ?


Solution

  • 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);