How to mock the HttpServletRequest? [duplicate]

孤者浪人 提交于 2019-12-04 15:07:03

问题


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:


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

   }
}



回答2:


Use some mocking framework e.g. Mockito or JMock which comes with mocking capacity of such objects.

In Mockito, you can do mocking as:

 HttpServletRequest  mockedRequest = Mockito.mock(HttpServletRequest.class);

For details on Mockito, see: How do I drink it? on the Mockito site.

In JMock, you can do mocking as :

 Mockery context = new Mockery();
 HttpServletRequest  mockedRequest = context.mock(HttpServletRequest.class);

For details on jMock, please refer: jMock - Getting Started




回答3:


This is an old thread ... but the question is still relevant.

Another good choice is MockServiceRequest and MockServiceResponse in the Spring framework:

http://docs.spring.io/spring/docs/2.0.x/api/org/springframework/mock/web/package-summary.html




回答4:


I don't know about easymock, but the book 'Unit Testing in Java: How Tests Drive the Code' by Johannes Link contained explanations of how to test Servlets using a library he'd build of dummy objects.

The companion site for the book is now gone (change in publishing company of something...) but the companion site from the original german publication is still up. From it, you can download the definitions of all the dummy objects.




回答5:


Have a look at Mockrunner: http://mockrunner.sourceforge.net/

It has a lot of easy to use Java EE mocks, including HttpServletRequest and HttpServletResponse.



来源:https://stackoverflow.com/questions/12945907/how-to-mock-the-httpservletrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!