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 8698035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:39:22+00:00 2026-06-13T01:39:22+00:00

I have a function that looks for a query parameter and returns a boolean:

  • 0

I have a function that looks for a query parameter and returns a boolean:

  public static Boolean getBooleanFromRequest(HttpServletRequest request, String key) {
        Boolean keyValue = false;
        if(request.getParameter(key) != null) {
            String value = request.getParameter(key);
            if(keyValue == null) {
                keyValue = false;
            }
            else {
                if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("1")) {
                    keyValue = true;
                }
            }
        }
        return keyValue;
    }

I have both junit and easymock in my pom.xml, how do I go about mocking the HttpServletRequest ?

  • 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-06-13T01:39:23+00:00Added an answer on June 13, 2026 at 1:39 am

    HttpServletRequest is much like any other interface, so you can mock it by following the EasyMock Readme

    Here is an example of how to unit test your getBooleanFromRequest method

    // static import allows for more concise code (createMock etc.)
    import static org.easymock.EasyMock.*;
    
    // other imports omitted
    
    public class MyServletMock
    {
       @Test
       public void test1()
       {
          // Step 1 - create the mock object
          HttpServletRequest req = createMock(HttpServletRequest.class);
    
          // Step 2 - record the expected behavior
    
          // to test true, expect to be called with "param1" and if so return true
          // Note that the method under test calls getParameter twice (really
          // necessary?) so we must relax the restriction and program the mock
          // to allow this call either once or twice
          expect(req.getParameter("param1")).andReturn("true").times(1, 2);
    
          // program the mock to return false for param2
          expect(req.getParameter("param2")).andReturn("false").times(1, 2);
    
          // switch the mock to replay state
          replay(req);
    
          // now run the test.  The method will call getParameter twice
          Boolean bool1 = getBooleanFromRequest(req, "param1");
          assertTrue(bool1);
          Boolean bool2 = getBooleanFromRequest(req, "param2");
          assertFalse(bool2);
    
          // call one more time to watch test fail, just to liven things up
          // call was not programmed in the record phase so test blows up
          getBooleanFromRequest(req, "bogus");
    
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that accepts one parameter and returns a table/resultset. I want
I have a simplified function that looks like this: function(query) { myApi.exec('SomeCommand', function(response) {
I have a WebMethod that recives HTML as parameter somthing like this: Public Function
i have a function that looks like this. function startChatSession($items) { foreach ($test as
I have a simple function that looks at an incoming mySQL data type and
I have a autoload function that looks like so: function __autoload_HTTP_Client($class_name) { $HC =
I have a JavaScript function that looks like this: function recalculateWhatIfCurrentRate(aEle, aChangedLabel, recordID, yearEndCurrentRate)
I have a constexpr function that looks something like this: constexpr int foo(int bar)
I have code that looks like this: var baseClass = function() { // CODE
I have some code that looks like this: var MyObject = function () {

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.