From: Vincent M. <vm...@oc...> - 2001-09-06 08:03:29
|
Steve, I am trying to find out if a generic way of writing mocks is possible and if a tool like MockMaker would be good enough. My question is thus: what would be the algorithm for generating a mock implementation of ServletOutputStream ? Thanks -Vincent ----- Original Message ----- From: "Steve Freeman" <st...@m3...> To: "Vincent Massol" <vm...@oc...> Sent: Wednesday, September 05, 2001 6:28 PM Subject: Re: [Mockobjects-java-users] What is the correct way of writing this test ? > This is example is OK as a very first cut at writing a servlet test. You would expect some kind of intermediate objects start to appear as soon as things became much more complicated. You certainly can't look for expected writes because of potential bufferings in the stream. > > setExpectedContent is not much different for a string than just getting it at the end. One thing I have worked on is a setExpectedSegment, which checks if a particular (small) substring is present. Either way, you should get away from testing strings like this as soon as you can, but you need to start somewhere. > > Steve > > ----- Original Message ----- > From: "Vincent Massol" <vm...@oc...> > To: <moc...@li...> > Sent: Wednesday, September 05, 2001 5:40 PM > Subject: [Mockobjects-java-users] What is the correct way of writing this test ? > > > > Hi, > > > > I have a simpe servlet with a doGet() method that simply prints a text to > > the output stream. I would like to know what is the correct way of writing a > > unit test for that method. It seems the suggested way (from the Mock Object > > sample and source code of MockHttpServletResponse) is to write: > > > > { > > SampleServlet servlet = new SampleServlet(); > > MockHttpServletRequest request = new MockHttpServletRequest(); > > MockHttpServletResponse response = new MockHttpServletResponse(); > > > > servlet.doGet(request, response); > > > > String expected = "<h1>A request</h1>"; > > String result = response.getOutputStreamContents(); > > assertEquals(expected, result); > > } > > > > However, I don't believe this is the canonical way of writing this test. I > > think > > it would be more appropriate and consistent to write (even if longer) : > > > > { > > SampleServlet servlet = new SampleServlet(); > > MockHttpServletRequest request = new MockHttpServletRequest(); > > MockHttpServletResponse response = new MockHttpServletResponse(); > > > > MockServletOutputStream output = new MockServletOutputStream(); > > > > output.setExpectedContent("<h1>A request</h1>"); > > response.setupOutputStream(output); > > > > servlet.doGet(request, response); > > > > output.verify(); > > } > > > > Question 1 : What do you think ? Should the example be changed ? Should the > > MockHttpServletResponse.getOutputStreamContents() method be removed ? > > Question 2 : Is it possible to automatically generate a mock of > > ServletOutputStream ? It would certainly not generate a setExpectedContent() > > method, right ? It would maybe generate a setExpectedWrite() method but that > > would not represent the full buffer... ServletOutputStream is an object > > that gets written to by the method under test, which means we need some > > accessors to get back the data. How can Mock Maker generate these accessors > > in a generic way ? > > > > Thanks > > -Vincent > > > > > > _______________________________________________ > > Mockobjects-java-users mailing list > > Moc...@li... > > https://lists.sourceforge.net/lists/listinfo/mockobjects-java-users > > > |