From: Francois B. <fb...@us...> - 2003-06-03 18:01:53
|
Hello everyone, I started using 0.09 and I hit a snag. The class under test is an implementation of ContextListener. I am testing the destruction of the context. I would like each of my tests to test the minimum amount of code possible. So, I would like to have a test that confirms that an attribute is removed from the context, and a second one to ensure that the DB is closed. So, here's the code at the moment: public class TestContextListener extends TestCase { private final ContextListener contextListener = new ContextListener(); private final Mock mockServletContext = new Mock(ServletContext.class); public void testRemovesTeamDBOnDestruction() { mockServletContext.expect("removeAttribute", C.eq("teamsdb")); ServletContext servletContext = (ServletContext)mockServletContext.proxy(); ServletContextEvent event = new ServletContextEvent(servletContext); contextListener.contextDestroyed(event); mockServletContext.verify(); } public void testClosesDBOnDestruction() { TeamDB db = new TeamDB(); // Need to check close with Mock mockServletContext.expectAndReturn("getAttribute", C.NO_ARGS, db); ServletContext servletContext = (ServletContext)mockServletContext.proxy(); ServletContextEvent event = new ServletContextEvent(servletContext); contextListener.contextDestroyed(event); mockServletContext.verify(); } } testRemovesTeamDBOnDestruction() passes, since I implemented things correctly. But testClosesDBOnDestruction() does not, as the contextListener calls removeAttribute(). But I already know that, since the previous test asserts this very thing. In my case, I would like that to not be an assertion failure. I could always join both tests and call it "testRemovesTeamDB_And_ClosesDBOnDestruction()", but I prefer the two simpler methods. Is there a way to make a Mock accept any method call that has no return value and just silently ignore it ? I believe EasyMock allowed something like that and that it was called "Nice Mock". Thanks ! Francois -- Francois Beausoleil Developer of Java Gui Builder http://jgb.sourceforge.net/ |