This post is an entry in my ongoing series about web integration testing with Jetty. See post 1, 2 and 3 in the series for more information.
I often find that a web interface needs to let a user select one of a list of objects as a relation to the object he is editing. This is the basic and simple way of dealing with many-to-one relationships. However, most web frameworks don’t make this as easy or well-documented as it should be. In this post, I will describe how to make a select-field with Spring-MVC and FreeMarker.
As always, I will start with a test:
public void testEditParent() {
// Create a hierarchy of test categories
Category parentCategory = new Category("parent category " + System.currentTimeMillis());
Category subcategory = parentCategory.addSubcategory("first subcategory " + System.currentTimeMillis());
Category newParent = new Category("new parent category " + System.currentTimeMillis());
categoryDao.insert(parentCategory);
categoryDao.insert(subcategory);
categoryDao.insert(newParent);
// go to the subcategory
beginAt("/category/edit.html?id=" + subcategory.getId()));
assertOptionEquals("parent", subcategory.getParent().getName());
// Update the parent field
selectOption("parent", newParent.getName());
submit();
assertTextInElement("parent", newParent.getName());
// Check that the new parent has the subcategory
beginAt("/category/edit.html?id=" + extrasubcategory.getId()));
assertTextInTable("subcategories", subcategory.getName());
// Check that the old parent no longer has the subcategory
beginAt("/category/edit.html?id=" + parentCategory.getId()));
assertTextNotInTable("subcategories", subcategory.getName());
}
Read the rest of this entry »