You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Jordan <jo...@libero.it> on 2017/07/06 13:06:02 UTC
Empty model when testing DropDownChoice with
wantOnSelectionChangedNotifications enabled
Hello. I am trying to test a custom Wicket page and I am stuck when trying to
submit a form. I have isolated a minimal working example.
The problem seems to be the wantOnSelectionChangedNotifications method of
the DropDownChoice. If it's set to true, the test prints out "Model = null",
otherwise it prints the correct solution "Model = choice2". Reading the
documentation I understand that when the DropDown's selection is changed, a
roundtrip is sent to update the DropDown's model, but where does the null
come from?
Below you can find the code. Thank you in advance.
*DropDownPage.java*
package pages;
import java.util.ArrayList;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
public class DropDownPage extends WebPage {
private Model<String> model;
public DropDownPage() {
Form form = new Form("form") {
protected void onSubmit() {
System.out.println("Model = " + model.getObject());
}
};
add(form);
ArrayList<String> choices = new ArrayList<String>();
choices.add("choice1");
choices.add("choice2");
model = new Model<String>("choice1");
DropDownChoice<String> dropDown = new DropDownChoice<String>("dropDown",
model, choices) {
@Override
protected void onSelectionChanged(String newSelection) {
}
@Override
protected boolean wantOnSelectionChangedNotifications() {
return true;
}
};
form.add(dropDown);
form.add(new Button("button", new Model<String>("Submit")));
}
}
*DropDownPage.html*
<html>
<body>
<form wicket:id='form'>
<select wicket:id='dropDown'>
<option></option>
</select>
<input type='submit' wicket:id='button'></input>
</form>
</body>
</html>
*DropDownPageTest.java*
package pages;
import static org.junit.Assert.*;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
public class DropDownPageTest {
private WicketTester tester;
@Before
public void setUp() throws Exception {
tester = new WicketTester();
}
@Test
public void test() {
tester.startPage(DropDownPage.class);
tester.assertRenderedPage(DropDownPage.class);
FormTester formTester = tester.newFormTester("form");
formTester.select("dropDown", 1);
formTester.submit();
}
}
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Empty-model-when-testing-DropDownChoice-with-wantOnSelectionChangedNotifications-enabled-tp4678205.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Empty model when testing DropDownChoice with
wantOnSelectionChangedNotifications enabled
Posted by Jordan <jo...@libero.it>.
Hello.
It worked! Thank you so much for your answer.
--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Empty-model-when-testing-DropDownChoice-with-wantOnSelectionChangedNotifications-enabled-tp4678205p4678210.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Empty model when testing DropDownChoice with
wantOnSelectionChangedNotifications enabled
Posted by Sven Meier <sv...@meiers.net>.
Hi,
> FormTester formTester = tester.newFormTester("form")
> formTester.select("dropDown", 1);
> formTester.submit();
by calling select() you're sending the first request with all values in
the form. submit() then submits a second empty request.
Do it this way:
> FormTester formTester = tester.newFormTester("form")
> formTester.select("dropDown", 1);
> formTester = tester.newFormTester("form")
> formTester.submit();
Have fun
Sven
Am 06.07.2017 um 15:06 schrieb Jordan:
> Hello. I am trying to test a custom Wicket page and I am stuck when trying to
> submit a form. I have isolated a minimal working example.
> The problem seems to be the wantOnSelectionChangedNotifications method of
> the DropDownChoice. If it's set to true, the test prints out "Model = null",
> otherwise it prints the correct solution "Model = choice2". Reading the
> documentation I understand that when the DropDown's selection is changed, a
> roundtrip is sent to update the DropDown's model, but where does the null
> come from?
>
> Below you can find the code. Thank you in advance.
>
> *DropDownPage.java*
>
> package pages;
>
> import java.util.ArrayList;
>
> import org.apache.wicket.markup.html.WebPage;
> import org.apache.wicket.markup.html.form.Button;
> import org.apache.wicket.markup.html.form.DropDownChoice;
> import org.apache.wicket.markup.html.form.Form;
> import org.apache.wicket.model.IModel;
> import org.apache.wicket.model.Model;
> import org.apache.wicket.request.mapper.parameter.PageParameters;
>
> public class DropDownPage extends WebPage {
>
> private Model<String> model;
>
> public DropDownPage() {
>
> Form form = new Form("form") {
> protected void onSubmit() {
> System.out.println("Model = " + model.getObject());
> }
> };
> add(form);
>
> ArrayList<String> choices = new ArrayList<String>();
> choices.add("choice1");
> choices.add("choice2");
>
> model = new Model<String>("choice1");
> DropDownChoice<String> dropDown = new DropDownChoice<String>("dropDown",
> model, choices) {
> @Override
> protected void onSelectionChanged(String newSelection) {
>
> }
>
> @Override
> protected boolean wantOnSelectionChangedNotifications() {
> return true;
> }
> };
> form.add(dropDown);
>
> form.add(new Button("button", new Model<String>("Submit")));
> }
> }
>
>
> *DropDownPage.html*
>
> <html>
> <body>
> <form wicket:id='form'>
>
> <select wicket:id='dropDown'>
> <option></option>
> </select>
>
> <input type='submit' wicket:id='button'></input>
> </form>
> </body>
>
> </html>
>
> *DropDownPageTest.java*
>
> package pages;
>
> import static org.junit.Assert.*;
>
> import org.apache.wicket.util.tester.FormTester;
> import org.apache.wicket.util.tester.WicketTester;
> import org.junit.Before;
> import org.junit.Test;
>
> public class DropDownPageTest {
>
> private WicketTester tester;
>
> @Before
> public void setUp() throws Exception {
> tester = new WicketTester();
> }
>
> @Test
> public void test() {
> tester.startPage(DropDownPage.class);
> tester.assertRenderedPage(DropDownPage.class);
>
> FormTester formTester = tester.newFormTester("form");
> formTester.select("dropDown", 1);
> formTester.submit();
> }
>
> }
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/Empty-model-when-testing-DropDownChoice-with-wantOnSelectionChangedNotifications-enabled-tp4678205.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org