Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create HttpServletRequest?

13,153 views
Skip to first unread message

Carfield Yim

unread,
Jan 3, 2003, 2:13:03 AM1/3/03
to
I would like to see if there anyway to create an instance of
HttpServletRequest, so that I can write unit test of method that need
create HttpServletRequest?

I like to test some method like interface like:

public void generate(Connection con, HttpServletRequest req,
ResourceBundle bundle) throws SQLException {

Michael Borgwardt

unread,
Jan 3, 2003, 5:24:39 AM1/3/03
to
Carfield Yim wrote:
> I would like to see if there anyway to create an instance of
> HttpServletRequest, so that I can write unit test of method that need
> create HttpServletRequest?

As HttpServletRequest is an interface, you're free to write an implementation
that does whatever you want. Could be a lot of work though. You might
be able to use the internal implementation classes of Tomcat or Jetty
or whatever.

Frank Breuer

unread,
Jan 3, 2003, 5:40:45 AM1/3/03
to
"Carfield Yim" wrote:

> I would like to see if there anyway to create an instance of
> HttpServletRequest, so that I can write unit test of method that
> need create HttpServletRequest?
>
> I like to test some method like interface like:

You might want to look at Cactus

http://jakarta.apache.org/cactus/index.html

instead of writing your own.

Christian Robert

unread,
Jan 3, 2003, 6:03:18 AM1/3/03
to
* Carfield Yim wrote: *

> I would like to see if there anyway to create an instance of
> HttpServletRequest, so that I can write unit test of method that need
> create HttpServletRequest?

Well, since it wouldn't make a lot of sense to just create a
HttpServletRequest "somewhere" and then simply pass it to your method
you might want to look to the Mockobjects site (www.mockobjects.com)

Christian

Jeff Robertson

unread,
Jan 3, 2003, 8:27:36 AM1/3/03
to
Carfield Yim <carf...@desktop.carfield.com.hk> wrote in message news:<av3d0p$bjjco$1...@ID-139807.news.dfncis.de>...

> I would like to see if there anyway to create an instance of
> HttpServletRequest, so that I can write unit test of method that need
> create HttpServletRequest?
>

HttpServletRequest is an interface so you can certainly implement it
yourself.

You probably want to check out Cactus
(http://jakarta.apache.org/cactus) or something similar before going
too far with this.

Ryan Breidenbach

unread,
Jan 3, 2003, 9:40:52 AM1/3/03
to
Carfield Yim <carf...@desktop.carfield.com.hk> wrote in message news:<av3d0p$bjjco$1...@ID-139807.news.dfncis.de>...

public class MyClass implements HttpServletRequest {
// implement the HttpServletRequest methods you are calling, just
// leave empty implementations for the rest. this is a "mock object"
}

Jeff Lewis

unread,
Jan 3, 2003, 11:34:21 AM1/3/03
to
Might want to check out apache jakarta's cactus:

http://jakarta.apache.org/cactus

'Cactus is a simple test framework for unit testing server-side java
code (Servlets, EJBs, Tag Libs, Filters, ...).'

Cheers,

Jeff

Carfield Yim <carf...@desktop.carfield.com.hk> wrote in message news:<av3d0p$bjjco$1...@ID-139807.news.dfncis.de>...

Andrea LN Spinelli

unread,
Jan 17, 2003, 6:58:07 AM1/17/03
to

Of course you could do
class MyHTS implements HttpServletRequest {
I did, and it worked; however, a visit to the junit site
proved me wrong: if you are testing at this level,
most probably your architecture is less-than-perfect.

Now my servlet is just a wrapper for extracting parameters
from the HttpServletRequest and putting them in HashMap,
which get passed to the
class doing the real work. The output go to an
OutputStream, which I save in a string and test againts
the desired output.

I do unit test on that worker class, not on the servlet.

Of course, you still need to unit test the servlet,
but I think this is best obtained calling
(from a unit test) the Application Server
on the servlet (java.net.URL), using a stub worker class,
and testing for output. You should check for
malformed URLs, GET vs POST, buffer overflows
(what is the effect of a 1MB parameter value?)
Another thing to test is raising an exception
from the worker class. Note that, really, you
are testing the application server here.

Just my two cents
andrea

--
Andrea Spinelli
IT&T, Via Sigismondi 40, Villa d'Alme', BG, Italy
e-mail: aspinelli@-deletehere-imteam.it phone: +39-035-636029
"Truth hurts, but pimples much more!"

0 new messages