title: How do you get JUnit to model multiple tabs/windows opened but only one session
body: <p>I'm writing unit tests for Spring Framework 2.5 using JUnit.  More specifically, I'm using the MockHttpServletRequest to test my controllers.  I'm running into a problem, testing a particular scenario.</p>
  
  <p>An app was editing objects, persisted to Hibernate.  The objects work something like this:</p>
  
  <pre><code>Class Workflow {
    ... various properties and methods
    Long workflowId;  // primary key for this object
    Long subclassId;  // foreign key to another class
    String subclassType; // what kind of other class?
  }
  
  Class Request {
    .. various other properties and methods
    Long subclassId; // primary key for this object
    Long workflowId; // foreign key to Workflow
  }
  </code></pre>
  
  <p>Workflow is always attached to one, and only one, Request and a Request is always attached to one, and only one Workflow.  However, there are multiple objects/tables which Workflow can link to, so the subclassType has to keep track of what other table to look in.  So, this is a little bit like Request inheriting from/implementing Workflow, but the database stores them in separate tables so the Java was implemented as separate classes.</p>
  
  <p>I didn't write this mess.  I'm maintaining it.  And it's in wide use, with several different applications and subclassTypes so there are limits to how much "cleanup" I can do.</p>
  
  <p>The web app in question was putting the Workflow object into HttpSession but editing the Request in a form.  When someone opened up multiple windows/tabs, with a different Request in each window, the Workflow object from the last one opened was overwriting any prior-opened ones (sessions are attached to cookies, only one set of cookies for the domain, shared by ALL windows in the web browser).  If they submitted the form for the first Request, the Workflow object from the last one (pulled from the HttpSession) was written with the submitted Request (NOT the correct one), corrupting the data in the database.</p>
  
  <p>We ended up with Requests which had multiple Workflows connecting to them, and Requests which had NO Workflows connecting to them.</p>
  
  <p>The obvious fix was to rewrite the app so it never stores the Workflow in the HttpSession.  workflowId is one of the fields in the Request; we persisted that to the form via a hidden field.  Yes, we hit the database more often, but we aren't cross-connecting our records.</p>
  
  <p>That's the situation.  Now, the question: how do you model this in JUnit? So that no one else, in the future, runs afoul of this situation again?</p>
  
  <p>We use a MockHttpServletRequest, plugging in form values and submitting them by using a AnnotationMethodHandlerAdapter and a Controller object.  As an example:</p>
  
  <pre><code>    MockHttpServletRequest request = new MockHttpServletRequest();
      MockHttpServletResponse response = new MockHttpServletResponse();
      AnnotationMethodHandlerAdapter handler = new AnnotationMethodHandlerAdaptor();
      RequestFormController controller = new RequestFormController();
  
      request.setMethod("GET");
      request.setRequestURI("/EditRequest.do"); // handled by the RequestFormController
  
      request.setParameter("wfId", Long.toString(wfid)); // wfId is the workflow ID of a valid Workflow with an attached Request
      ModelAndView mv = handler.handle(request, response, controller);
  
      request.setParameter("wfId", mv.getModel().get("wfId");
      request.setParameter("requestText", "blah blah blah");
  
      request.setMethod("POST");
      request.setRequestURI("/UpdateRequest.do"); // handled by the RequestFormController
      mv = handler.handle(request, response, controller);
  </code></pre>
  
  <p>This is all pretty straightforward, Spring/JUnit stuff.  The problem is that, to open something in two different tabs/windows, you have to have two different requests, each of which has its own HttpSession.  With a web browser, though, those two different requests share the same cookies, which means they share the same session information.  How do you get JUnit to model that?</p>
  
  <p>I can copy the cookies from one to the other, but that doesn't seem to expose the error.  MockHttpServletRequest is still keeping the HttpSession information separate.  I'm having a hard time copying the HttpSession Attributes from one to the other.  Insomuch as I've done that, I can't seem to get that to expose the error, either.</p>
  
  <p>Any suggestions? This is a very real problem, and MockHttpServletRequest and all the other JUnit-related things we're doing aren't, accurately, modeling the real world of Tomcat and web browsers.</p>
  
  <p>Peace.</p>