I like to test some method like interface like:
public void generate(Connection con, HttpServletRequest req,
ResourceBundle bundle) throws SQLException {
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.
> 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.
> 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
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.
public class MyClass implements HttpServletRequest {
// implement the HttpServletRequest methods you are calling, just
// leave empty implementations for the rest. this is a "mock object"
}
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>...
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!"