4

I have the following panel that I use in my wicket application and I want to write a test to ensure that calling addPatternValidator(String pattern) adds a PatternValidator to the TextField.

public class StringTextBoxPanel extends Panel {
  private static final long serialVersionUID = 1L;

  private String stringModel = new String();
  private TextField<String> textfield;
  private Label label;

  public StringTextBoxPanel(String id, String labelText) {
    super(id);
    label = new Label("label", labelText);
    textfield = new TextField<String>("textField", new PropertyModel<String>(this, "stringModel"));
    add(label);
    add(textfield);
  }

  public String getValue() {
    return textfield.getValue();
  }

  public void addPatternValidator(String pattern) {
    this.get(textfield.getId()).add(new PatternValidator(pattern));
  }

  public void setRequired() {
    textfield.setRequired(true);
  }
}

Is it possible to do that with WicketTester?

jrochette
  • 1,117
  • 5
  • 22

2 Answers2

3

Start a FormTester in WicketTester, "input" some illegal value to your TextField, have the FormTester submit the Form and

a) check the Model, invalid values aren't written to the model

b) check for error messages

But to tell the truth and offer my thoughts not asked for, I don't quite get, why you want to test this... The add method is part of wicket and shouldn't be tested by you but by the Wicket developers. The same applies for the PatternValidator class, you might want to test your pattern though. As for the rest of the code in this method... It's trivial and wouldn't justify a test as far as I'm concerned.

Appendix (as mentioned in the comment, there are easier ways to make sure, a method was called than to invoke a FormTester. This snippet was just hacked into this editor, so no IDE checks whatsoever were applied):

private Boolean methodCalled = false;

@Test
public void testSomething() {
    WicketTester tester = new WicketTester();
    tester.startComponentInPage(new StringTextBoxPanel("id", "someText") {

        @Override
        public void addPatternValidator(String pattern) {
            methodCalled = true;
        }
    });
    AssertTrue(methodCalled);
}
Nicktar
  • 5,548
  • 1
  • 28
  • 43
  • He is not testing the `add(IValidator)` method, he is testing if his code has correctly called it to add the validator, and it is working as expected. – tetsuo Aug 02 '12 at 11:18
  • @Nicktar thanks for the answer, I was able to test what I wanted to! – jrochette Aug 02 '12 at 12:04
  • @tetsuo Then a test to an (anonymous) Subclass with an overriden `addPatternValidator`method setting some boolean value that can be asserted would be a lot easier. I'll extend the answer (even if it's already accepted to demonstrate, what I want to say). – Nicktar Aug 03 '12 at 08:35
2

I checked for error/info messages after form submit:

wicketTester.assertHasNoErrorMessage();
wicketTester.assertHasNoInfoMessage();
wicketTester.getMessages(FeedbackMessage.FATAL); //or another kind of messages