Discussions

Web tier: servlets, JSP, Web frameworks: mock object query- The methods in MockHttpServletRequest class

  1. Hi all,
        I am doing Junit testing in collaboration with mock objects. I created a MockHttpServletRequest object.In that I am setting some parameters using the method setAttribute() and I tried to access the same parameter by using getAttribute().the object I set is not the one which i am retrieved through getattribute(). The code I am using is as below

    public void testRequestOnly()
    {
    MockHttpServletRequest request =new MockHttpServletRequest();
    String attr = "abcd";
    request.setAttribute("name", attr);
    Object returnObj=request.getAttribute("name");
    assertEquals(attr,returnObj);
    }

    I am getting the error "Assertion failed error: Attributes run out of objects"

    According to the basics, the object an setting should match with the one I am retrieving through getAttribute().


    If any body knows, please write me back
  2. you may want to try replacing

    Object returnObj=request.getAttribute("name");

    with

    String returnObj = (String) request.getAttribute("name");