user2194838
user2194838

Reputation: 357

Create the HttpServletRequest and HttpServletResponse message instead of mock

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

Answers (1)

Jacob Davis-Hansson
Jacob Davis-Hansson

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:

https://github.com/neo4j/neo4j/blob/3.2/community/server/src/main/java/org/neo4j/server/rest/web/InternalJettyServletRequest.java

Upvotes: 1

Related Questions