I've just started using HttpUnit to test JSPs, however I have come up against the problem of the session. I need to test JSPs that expect information in the session (i.e. within the jsp, session.getAttribute(".....") is used to grab some information), but I can't figure out how to actually place these objects in the session before the jsp is executed.
Is this a problem that HttpUnit cannot solve? (refactoring time?)
Cheers,
Joe.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you are using ServletUnit, (that ships with HTTPUnit), then you can
do something like this:
public void testPost() throws Exception {
request.setParameter("color", "amber");
InvocationContext ic = client.newInvocation(request);
BeerSelect beerServlet = (BeerSelect) ic.getServlet();
beerServlet.doPost(ic.getRequest(), ic.getResponse());
BeerExpert be = new BeerExpert();
List brands = be.getBrands("amber");
response = client.getResponse(request);
assertEquals(brands, ic.getRequest().getAttribute("styles"));
assertEquals("jsp forward", "result.jsp", response.getTitle());
}
My servlet doPost code uses the request parameter to pass a value to
the BeerExpert, (model component), that returns some advice on beer,
the result of this is then added as an attribute value that is then
accessible to the jsp.
If you want to set the attributes in your test code then you could do
something like the following:
public void testGetColorAndSetStylesForAmber() throws Exception {
request.setParameter("color", "amber");
InvocationContext ic = client.newInvocation(request);
BeerSelect beerServlet = (BeerSelect) ic.getServlet();
beerServlet.setStyles(ic.getRequest(), beerServlet.getColor(ic
.getRequest()));
BeerExpert be = new BeerExpert();
List brands = be.getBrands("amber");
assertEquals(brands, ic.getRequest().getAttribute("styles"));
}
(Sorry for the mickey mouse examples, but I am preparing for my SCWCD
exam and thought it would be fun to do so using TDD).
Hope this helps
Aldred
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Guys,
I've just started using HttpUnit to test JSPs, however I have come up against the problem of the session. I need to test JSPs that expect information in the session (i.e. within the jsp, session.getAttribute(".....") is used to grab some information), but I can't figure out how to actually place these objects in the session before the jsp is executed.
Is this a problem that HttpUnit cannot solve? (refactoring time?)
Cheers,
Joe.
Joe,
If you are using ServletUnit, (that ships with HTTPUnit), then you can
do something like this:
public void testPost() throws Exception {
request.setParameter("color", "amber");
InvocationContext ic = client.newInvocation(request);
BeerSelect beerServlet = (BeerSelect) ic.getServlet();
beerServlet.doPost(ic.getRequest(), ic.getResponse());
BeerExpert be = new BeerExpert();
List brands = be.getBrands("amber");
response = client.getResponse(request);
assertEquals(brands, ic.getRequest().getAttribute("styles"));
assertEquals("jsp forward", "result.jsp", response.getTitle());
}
My servlet doPost code uses the request parameter to pass a value to
the BeerExpert, (model component), that returns some advice on beer,
the result of this is then added as an attribute value that is then
accessible to the jsp.
If you want to set the attributes in your test code then you could do
something like the following:
public void testGetColorAndSetStylesForAmber() throws Exception {
request.setParameter("color", "amber");
InvocationContext ic = client.newInvocation(request);
BeerSelect beerServlet = (BeerSelect) ic.getServlet();
beerServlet.setStyles(ic.getRequest(), beerServlet.getColor(ic
.getRequest()));
BeerExpert be = new BeerExpert();
List brands = be.getBrands("amber");
assertEquals(brands, ic.getRequest().getAttribute("styles"));
}
(Sorry for the mickey mouse examples, but I am preparing for my SCWCD
exam and thought it would be fun to do so using TDD).
Hope this helps
Aldred