jrochette
jrochette

Reputation: 1117

Using WicketTester to verify a Validator is added to a TextField?

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?

Upvotes: 4

Views: 1904

Answers (2)

Nicktar
Nicktar

Reputation: 5575

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

Upvotes: 3

Grigorichev Denis
Grigorichev Denis

Reputation: 329

I checked for error/info messages after form submit:

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

Upvotes: 2

Related Questions