Hi! Yes, separating the forwarding from the rest is definitely a solution. Thanks very much for the help Kazuhito and Nicolas!
/Niklas On Mon, 15 Aug 2005, Kazuhito SUGURI wrote: > Hi Niklas, > > In article <[EMAIL PROTECTED]>, > Sat, 13 Aug 2005 12:24:54 +0200 (CEST), > Niklas Lindholm <[EMAIL PROTECTED]> wrote: > cactuslist> > response.getWebRequest().getURL(); > cactuslist> > cactuslist> I have done a simple test and in that test the getURL() method > returns > cactuslist> null. So I get this failure: > cactuslist> > cactuslist> junit.framework.AssertionFailedError: expected:<unit.jsp> but > was:<null> > cactuslist> > cactuslist> I have a servlet that ends with this line: > cactuslist> > cactuslist> req.getRequestDispatcher("unit.jsp").forward(req, res); > cactuslist> > cactuslist> And in my endXXX() method I have this assertion: > cactuslist> > cactuslist> assertEquals("unit.jsp", response.getWebRequest().getURL()); > cactuslist> > cactuslist> The endXXX() method is run on the client side. Will the client > side really > cactuslist> have information about what forward that has been done since this > typs of > cactuslist> forward is done entirely on the server side? I would imagine that > this > cactuslist> kind of assertion would have to go into the testXXX() method. > > AFAIK, the getURL() method returns a simulated URL > which is specified by WebRequest#setURL method. > So, you cannot know a forwarded URL by using the method. > > > cactuslist> > Why ?? It seems a great solution : you can check more ie that > there is > cactuslist> > no error message in the page code... > cactuslist> > cactuslist> Well.. in this case I don't want to verify the content of the > resulting > cactuslist> page. Only the logic of the servlet. And it could be pretty > difficult to > cactuslist> figure out from the resulting HTML which jsp page that actually > has been > cactuslist> invoked. (Unless I code some comment into it telling which jsp > page it > cactuslist> is.. hmm.. that might be a solution..) > > If you are interested in the servlet logic, you might separate > the logic into some methods to make your test easier. For your case: > - a method returns String as a resource name to be forwarded > - a method forward the request to the selected resource > > For example, the code will be like as follows: > public String getForwardTo() { > // resource selection logic > return resourceName; > } > ... > req.getRequestDispatcher(getForwardTo()).forward(req, res); > > You can write tests for the getForwardTo() method to verify > the selection has been done properly. > > If you want the forward is been done properly, > you have to verify the resulting HTML in endXXX(). > To make it easier, you might insert ID string as a HTML comment > in every JSPs. > > Hope this helps, > ---- > Kazuhito SUGURI > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]