• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simulation of HTTP Post request with message body

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am trying write a junit style test to invoke a Struts 2 action class, using Spring 2.5. I have been given a message body of an HTTP Post request that will be sent to our web site from an external source. This message body is plain text exactly of the Post request itself. I want to write a test against this.

I have been looking at the different Java web testing frameworks like JWebUnit, HtmlUnit and HttpUnit but none seem to do what I want.

I do not want either:

or

What I want is:

where myMessageBody is a plain-text concatenation of all the fields, which my test (Tomcat / Struts / Spring) has to unpack and deal with. Why do I want it this way? Because that is the API specification I am given and what I must program against.

Any ideas?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change the requirement--you're now testing the web server, Struts 2, *and* your code. The purpose of unit testing is to test small, standalone units, that (hopefully) don't depend on too many other things. Besides--you'd have to run that unit test inside a container: Struts 2 uses the container's request processing to access request parameters.

This is almost certainly not what you want to do.
 
Raj Nagappan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

Yes I understand that. But I am specifically given a text file which is exactly what the Post request will look like. I need to test directly against this text file that contains all the fields. I have other unit tests that test isolated parts. But I really need this automated acceptance test that says "send this Post message text to me (to simulate a page transmission over the net) and I will respond correctly". How can I do this?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the java.net.HttpURLConnection class directly to send the request.
 
Raj Nagappan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf I'll give it a try
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll still need to either run the test against a running container (an integration test), or simulate everything up to the point of the action request. IMO this isn't a good way to do this, since you're basically testing the app server. That's a slippery slope. If you're that uncomfortable with your app server, I'd consider maybe using a different one.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic