I spent the better part of a day putting together an archetype for a Java web application combining Spring 3 (MVC), Spring 3 (Test), UrlRewriteFilter, iBatis and SiteMesh. I am using Spring's annotation based configuration for web controllers and was pleasantly surprised/impressed with their corresponding test mechanism.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/com/domain/app/conf/spring.sb.xml")
public class TestQuickStartController
{
@Autowired
private QuickStartController controller;
@Before
public void setup()
{
// Dependency2 d1 = new Dependency1();
// Dependency3 d2 = new Dependency2(d1);
// Dependency3 d3 = new Dependency3(d2);
// controller = new QuickStartController(d3);
}
@SuppressWarnings("unchecked")
@Test
public void testQuickStart()
{
final Model model = new BindingAwareModelMap();
final String m1 = "hello";
final String dest = controller.quickStart(m1, model);
// test the destination view
Assert.assertEquals("quickstart/example", dest);
// test the message in the model
final String m2 = (String) model.asMap().get("message");
Assert.assertEquals(m1, m2);
// test for the user(s)
final List<User> us = (List<User>) model.asMap().get("users");
Assert.assertNotNull(us);
Assert.assertEquals(1, us.size());
}
}I find this to be a pretty slick way to initialize a controller as if it were running in a Spring container.