Using WicketTester to validate Validator added to TextField?

I have the following panel that I use in my gated application, and I want to write a test to make sure 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); } } 

Can this be done using WicketTester?

+4
source share
2 answers

Run FormTester in WicketTester, "enter" the incorrect value in your TextField, submit the FormTester form and

a) check the model, invalid values โ€‹โ€‹will not be written to the model

b) check for error messages

But to tell the truth and suggest my thoughts that were not asked, I donโ€™t quite understand why you want to check it ... The adding method is part of the gate and should not be checked by you, but the Developers of the gate. The same applies to the PatternValidator class, however you can check your pattern. As for the rest of the code in this method ... This is trivial and does not justify the verification as far as I know.

Application (as mentioned in the commentary, there are simpler ways to make sure that a method has been called than to call FormTester. This snippet was just hacked into this editor, so no IDE checks which ones are 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); } 
+3
source

I checked the error messages / messages after the submit form:

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

All Articles