How to Create a Dummy Request and Response for Servl开发者_运维知识库et Testing.I read about the Apache Jkarta Cactus Project but do not want to go in that much details. Can we just construct a dummy request response and pass it to doGet() method (Ofcourse the request constructed would have a URL and request parameters, but how to construct such dummy request)?
and also want no dependency of junit on Apache(should run independent of it).
Look into MockHTTPServletRequest(), I think its going to work. Here is the link which you can read.
HttpServletRequest
and HttpServletResponse
are just interfaces, so you can easily implement them yourself and use them in unit tests.
Of course, the various methods on these interfaces interrelate, especially in the request, so it's trickier than it first looks.
Some frameworks come with test implementations of those interfaces. For example Spring comes with MockHttpServletRequest
and MockHttpServletResponse
classes which have comprehensive sensible implementations of these interfaces.
Alternatively, you can use a mock object framework (of which Java has many... jmock, mockito, easymock to name but 3) which allow you to just implement the methods you're interested in.
精彩评论