From: Jeff D. <Jef...@no...> - 2003-04-30 22:47:10
|
I'm using MockHttpSession in a unit test I bit confused on the proper usage. In here is my setUp method public void setUp() throws Exception { super.setUp(); _request = new MockHttpServletRequest(); _response = new MockHttpServletResponse(); _session = new MockHttpSession(); _request.setSession(_session); _context = new VelocityContext(); } Here is my unitTest public void testFreeUserStandByPage() throws ExecuteCommandException { IUser user = new MockFreeUser(); assertNotNull("We must have a user for our test to work", user); _request.setupAddParameter("VARVALUE", "1880-10"); // Add User to the session _session.setAttribute("user", user); // our expected session attributes to be used by validate _session.setExpectedAttribute("VARVALUE", "1880-10"); _session.setExpectedAttribute("noStandby", Boolean.TRUE); _session.setExpectedAttribute("user", user); // run the command Command command = new CreateWssrdPdfDocumentCommand(_request, _response); String templateName = command.exec(_context); // check the results IUser testUser = (IUser) _context.get(IUser.USER_KEY); assertEquals(user, testUser); _session.verify(); assertEquals("Make sure we are getting the standby page template", templateName, "standby.vm"); } What is weird is that the order of setAttibute and setExpectAttribute effect the success of my test. In the previous code the test fails, because user object is not found in the session during the verify() process. My test succeeds when I move the _session.setAttribute("user,user) statement after the setExecptedAttribute methods. This would be fine, but my next test fails both ways. The problem is that I have to send the session attribute noStandby to Boolean.TRUE. When command is completed I had expect that the attribute would not be in the session, but this failed in the test. jUnit was always seeing the attribute. Thus, I refactored my code to change it to the attribute noStandby to be Boolean.TRUE. The problem is that I get an exception in my test code on my _session.setAttribute("noStandby", Boolean.FALSE) saying that is unexpected. I confused. Any help or suggestions would be appreciated. |