3

I need to write unit test around a function which takes HttpServletRequest and HttpServletResponse object as an argument.

If I create the mock of these two object(request and response), how change in one will reflect in another.

Ex. If I want to unit test around the code where I am setting the header in response object.

response.addHeader("X-UA-Compatible", "IE=EmulateIE7");

Let me know how to proceed .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
shantanu
  • 1,748
  • 3
  • 19
  • 34

3 Answers3

0

In your mocking library there will be some way to assert that a method has been invoked. So, your test code would include a statement such as:

// psuedo-code
assertThat(mockResponse).addHeader("X-UA-Compatible", "IE=EmulateIE7");

In the mocking frameworks I have used (JMock, Mockito), there is no automatic co-operation between mocked objects. So your mocked HttpServletRequest will have no relationship to your mocked HttpServletResponse unless you declare one.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

The Spring Framework provides both a MockHttpServletRequest and a MockHttpServletResponse.

0

The problem is with standard mocks, they will create you a proxy. So you may never know if the correct value was set after the code calls

response.addHeader("X-UA-Compatible", "IE=EmulateIE7");

If you create the stub yourself then you can actually verify what was set in the method call. But this approach has many problems as well.

Sajith Silva
  • 823
  • 1
  • 13
  • 24