Thanks for your help Geoff, it certainly clarifies alot of assumptions made so far. As for the below:
Also how do I handle an expectation such as: session.setAttribute("username", name); ?
I'm not sure if I understand the question; you could do:
one(session).setAttribute("username",name);
That assumes that you have a 'name' variable somewhere that the expectation can find, but otherwise it's fine.
I have the following lines of code in my servlet:
String name = request.getParameter("j_username");
HttpSession session = request.getSession();
session.setAttribute("name", name");
If I mock the objects as follows:
HttpSession session = mockery.mock(HttpSession.class);
HttpServletRequest request = mockery.mock(HttpServletRequest.class);
String name = "anthony";
mockery.checking(new Expectations() {{
allow(request).getSession();
will(returnValue(session));
allow(request).getParameter("j_username");
will(returnValue(name));
}});
Ergo, the code should use the "mocked" value for name as "anthony" for
String name = request.getParameter("j_username")
However running the code reveals an Expectation not being set for
session.setAttribute("name", name);
To set an expectation for the session.setAttribute:
String name = "anthony";
mockery.checking(new Expectations() {{
one(session).setAttribute("username",name);
}});
Doesn't really make sense as I would be somehow introducing a process that is not quite consistent, the value for "name" should be the one that is encapsulated inside the application login not "injected" as such.
In my test I would like to able to see: assertNotNull(session.getAttribute("name")); return a true value based on the value from the request, not the expectation. Confusing I know cause they are supposed to be the same value but the value is not part of the flow so to speak if I "Expect" it for the
session.setAttribute() method. Make sense?
Anthony