File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes how to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "how to "simulate" HTTP server" Watch "how to "simulate" HTTP server" New topic
Author

how to "simulate" HTTP server

Clay Bridges
Greenhorn

Joined: Jun 04, 2005
Posts: 4
I'm writing a chunk of a larger application, which, among other things, creates XHTML forms, and then is supposed to process whatever that form returns.

Obviously, I need to test this somehow.

For shipping code, I'm allowed to use J2SE 1.4.2 (note: not J2EE), and don't have free hand to free hand to add third party libraries to the project. For test code, I could, however, add another program for my own purposes, and then ship to my client seperately for his optional use.

If I use J2EE, and if it contains a built-in servlet container (I couldn't figure this out yet), I'm guessing I could likely write a simple servlet to play the middle man here (serving http, processing http get/post).

So, is there a way to do this without J2EE/3rd party libs, and if not, any pointers to the quick and dirty way to do this in J2EE.

Thanks,

Clay
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I don't yet understand what your code has to do, and what you want to test. Please elaborate.

Moving to Testing forum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
How does the application process the stuff the XHTML form returns? Does it use servlets?

J2EE does come with a "reference implementation" of a servlet container, but it would probably be simpler to use a different free implementation of a servlet container like Orion or JBoss or perhaps OC4J.
This page might be useful if you decide to go this route.

On the other hand, if you just want to test the input and output of the XHTML page, something along the lines of HTTPUnit might be useful.
[ June 05, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Clay Bridges
Greenhorn

Joined: Jun 04, 2005
Posts: 4
Originally posted by Marilyn de Queiroz:
How does the application process the stuff the XHTML form returns? Does it use servlets?


This is up to me to define. Probably something generalized to a Reader, depending on my formatting and parsing options. I'm presently outputting the XHTML form using a Writer.

Originally posted by Marilyn de Queiroz:

J2EE does come with a "reference implementation" of a servlet container, but it would probably be simpler to use a different free implementation of a servlet container like Orion or JBoss or perhaps OC4J.
This page might be useful if you decide to go this route.

On the other hand, if you just want to test the input and output of the XHTML page, something along the lines of HTTPUnit might be useful.

[ June 05, 2005: Message edited by: Marilyn de Queiroz ]


That's totally helpful. Thanks.
Clay Bridges
Greenhorn

Joined: Jun 04, 2005
Posts: 4
Originally posted by Ilja Preuss:
I don't yet understand what your code has to do, and what you want to test. Please elaborate.


I have a survey, represented in a java class structure, which I output to a Writer as an XHTML form. That's the UI, if you will. In another method, I'm supposed to take the GET/POST of that form, in a format TBD, and process it back into my class structure.

Writing a servlet is not part of my spec, but I need something which will "serve" this XHTML form, and hand me back the post/get data, so I can readily test (both the output of the form, and the processing of the input get/post data). I *can* load the form in a browser from a static file, but that's not going to give me data back, or at least I don't know how to do that.

Thanks,

Clay
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Clay,
It sounds like you want to test two things. Checking that the form is generated correctly sounds like something you have already done (through the browser.) The interesting part is how to test the processing get/post. The way to do this is through a mock objects framework such as easyMock or jMock. These let you create a pretend HttpServletRequest with whatever parameter values you want in as many combinations as you want.

You will probably want to keep these JUnit/mock object tests to make sure your code works in the future. That way you don't have to run all the tests by hand each time. But if you really want the tests to be interactive, you can prompt the user at the command line for the values for the mock objects to pass.

All this is unit testing. Once the servlet exists, you (or someone else) can do integration testing with the real servlet.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jeanne Boyarsky:
The way to do this is through a mock objects framework such as easyMock or jMock. These let you create a pretend HttpServletRequest with whatever parameter values you want in as many combinations as you want.


http://www.mockobjects.com/ provides predefined mockobjects for the Servlet API, which can be quite convenient.
Clay Bridges
Greenhorn

Joined: Jun 04, 2005
Posts: 4
Just wanted to post the denouement. I'm coding J2SE in Eclipse, and I needed a servlet to enable an interactive test of a java-generated XHTML form, the resultant POST/GET responses. For my purposes, the easiest way to do this turned out to be using the Jetty servlet container and the Jetty Launcher plugin for Eclipse. Dead easy, and IMO, way easier than Tomcat.

Thanks for all your help.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Clay Bridges:
Just wanted to post the denouement. I'm coding J2SE in Eclipse, and I needed a servlet to enable an interactive test of a java-generated XHTML form, the resultant POST/GET responses. For my purposes, the easiest way to do this turned out to be using the Jetty servlet container and the Jetty Launcher plugin for Eclipse. Dead easy, and IMO, way easier than Tomcat.

Note that you can also start and stop an embedded Jetty HttpServer in your JUnit tests.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
GeeCON Prague 2014
 
subject: how to "simulate" HTTP server