Reputation: 357
I am writing a unit test for filter to log request and response message. Can we create the HttpServletRequest and HttpServletResponse message instead of mock to send as a input to doFilter() method.
Upvotes: 1
Views: 2345
Reputation: 2663
Sure, just implement the interface. However, these are large interfaces, so it's a lot of code to maintain instead of the mock.
If your reason to not use a mock is a simple aversion to mocks (good!) note that there's really no difference between manually implementing a "mock" object and letting a framework do it for you; in both cases you're giving your code something different from what the web framework you end up using will use.
The code you're writing is perhaps better tested with integration tests - starting a real web server as part of your test harness and generating real HTTP requests.
If you are trying to solve an issue the mocks are creating for you by manually implementing the interface, here's an example of a custom HttpServletRequest implemented as an extension of the implementation that ships with Jetty:
Upvotes: 1