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-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to