Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6116447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:12:25+00:00 2026-05-23T15:12:25+00:00

I’m writing unit tests for Spring Framework 2.5 using JUnit. More specifically, I’m using

  • 0

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.

An app was editing objects, persisted to Hibernate. The objects work something like this:

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
}

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.

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.

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.

We ended up with Requests which had multiple Workflows connecting to them, and Requests which had NO Workflows connecting to them.

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.

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?

We use a MockHttpServletRequest, plugging in form values and submitting them by using a AnnotationMethodHandlerAdapter and a Controller object. As an example:

    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);

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?

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.

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.

Peace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:12:25+00:00Added an answer on May 23, 2026 at 3:12 pm

    When writing your test you can explicity create a session and set all requests to use the same session.

    MockHttpSession session = new MockHttpSession();
    request.setSession(session);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.