jakarta-cactus-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kazuhito SUGURI <suguri.kazuh...@lab.ntt.co.jp>
Subject Re: How to assert forwards?
Date Mon, 15 Aug 2005 03:43:28 GMT
Hi Niklas,

In article <Pine.LNX.4.58.0508131215060.28989@schlager.nilin.se>,
Sat, 13 Aug 2005 12:24:54 +0200 (CEST),
Niklas Lindholm <cactuslist@nilin.se> 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

Mime
View raw message