Re: [Httpunit-develop] Setting application context in a servlet.
Brought to you by:
russgold
From: Ken H. <ke...@nc...> - 2004-07-28 10:46:58
|
Andrew R. Rothwell wrote: >Crew, > >I am using HttpUnit within JUnit, and in conjunction with Spring. > >In my JUnit setUp() method I call ServletRunner with my web.xml >file. This results in 4 servlets being autoloaded. > >These servlets reference external beans which the code under >test needs. So I figured I'd create an application context by >loading these beans using Spring's FileSystemXmlApplicationContext() >constructor. > >What I need of course is a way to associate this context with each >of the autoloaded servlets. > >>From a review of the code it seems pretty clear that changes are >required to HttpUnit to support this sort of functionality. > >I was wondering if anyone has considered this issue, even developed >a solution for it, or at least have guidelines on how to proceed. > >I am not on the httpunit-develop list; please reply directly. > >Cheers, >Andrew. > > > > I'm assuming by 'application context' you're meaning ServletContext - I usually use a Mock object for the servlet context itself, and a subclass of the servlet under test that retrieves this mock object. I'll leave the creation of the MockServletContext to you - it's just an implementor of the ServletContext interface. Something like: class TestIt extends TestCase { public void test1() { MockServletContext ctx = new MockServletContext(); ctx.setAttribute("foo","bar"); // some data I want in the servlet context... MyServletSubclass testObject = new MyServletSubClass(ctx); assertXXX(testObject.somethingorother()); } private class MyServletSubClass extends MyServlet { private ServletContext _context = null; public MyServletSubClass(ServletContext context) { super(); _context = context; } public ServletContext getServletContext() { return _context; } // overrides method in superclass. } } Ken |