THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Code Block |
---|
public class TestHomePage extends TestCase { private WicketTester tester; @Override public void setUp() { tester = new WicketTester(new WicketApplication()); } /** * */ public void testBookList() { // start and render the test page tester.startPage(HomePage.class); // assert rendered page class tester.assertRenderedPage(HomePage.class); // assert rendered label component tester.assertLabel("message", "If you see this message wicket is properly configured and running"); // assert rendered page class HomePage homePage = (HomePage) tester.getLastRenderedPage(); BookForm bookForm = homePage.getForm(); BookListView bookListView = bookForm.getBookListView(); List<BookListItem> bookItems = toLinkedList(bookListView.iterator()); { // Test values of 1st book BookListItem bookListItem = bookItems.get(0); // Note: no hassle with run-time component path id's assertEquals("first", bookListItem.getNameField().getValue()); assertEquals("", bookListItem.getAuthorField().getValue()); assertEquals("-1", bookListItem.getTypeChoice().getValue()); } { // Test values of 2nd book BookListItem bookListItem = bookItems.get(1); // Note: no hassle with run-time component path id's assertEquals("second", bookListItem.getNameField().getValue()); assertEquals("", bookListItem.getAuthorField().getValue()); assertEquals("-1", bookListItem.getTypeChoice().getValue()); } { // Test values of 3rd book BookListItem bookListItem = bookItems.get(2); // Note: no hassle with // run-time component path // id's assertEquals("third", bookListItem.getNameField().getValue()); assertEquals("", bookListItem.getAuthorField().getValue()); assertEquals("-1", bookListItem.getTypeChoice().getValue()); } { // Set new values FormTester formTester = tester.newFormTester(bookForm .getPageRelativePath()); // formTester.setValue(bookItems.get(1).getAuthorField(), "Hemingway"); // TODO New feature request tester.getServletRequest().setParameter( bookItems.get(1).getAuthorField().getInputName(), "Hemingway"); // Submit changes // formTester.submit(bookForm.getSubmitButton()); // TODO New feature request formTester.submit(); } tester.assertRenderedPage(HomePage.class); // Verify the submitted value { // Test values of 2nd book BookListItem bookListItem = bookItems.get(1); // Note: no hassle with run-time component path id's assertEquals("second", bookListItem.getNameField().getValue()); assertEquals("Hemingway", bookListItem.getAuthorField().getValue()); assertEquals("-1", bookListItem.getTypeChoice().getValue()); } } /** * @param <T> * @param iterator * @return List<T> */ public static <T> List<T> toLinkedList(Iterator<T> iterator) { List<T> linkedList = new LinkedList<T>(); for (; iterator.hasNext();) { linkedList.add(iterator.next()); } return linkedList; } } |
...