Ways to test RESTful services? [closed]
Asked Answered
G

5

26

I want to test my RESTful applications directly via HTTP and I am looking for tools that can help me with that task. Basically I am looking for an easy wrapper for HTTP requests that can submit e.g. HTML forms or serialized resources as JSON or XML.

It would be great if there is a way to verify if the service is actually following REST architectural guidelines (statelessness, URIs, content negotiation etc.), too.

Being able to use it with JUnit would be a convenient bonus. Do you know about any libraries that could help me with what I want to do (and that are a little more than just a simple http client)?

Green answered 29/1, 2010 at 22:15 Comment(8)
That's not really a "unit" test anymore. I don't think that word means anything anymore.Sweetandsour
True... I was still thinking the unit test way, changed it. Would be still nice to have Java integration for it though (actually unit testing will probably end up in a big hassle with Request and Response mocks and I somehow want to avoid that).Green
Don't forget, that unit testing doesn't dictate what is an unit. IMHO restful service can be seen as an unit if it is isolated enough - e.g. tested by smaller level unit tests.Powerhouse
I have used the Poster add-on for Firefox.Jimenez
I would consider using HTML UNIT - htmlunit.sourceforge.netBerley
Found a new project coming up: REST testing in Java on GitHubGreen
check out fiddler2.com/fiddler2 , the best option I have used.Faun
I use httpshell which lets you issue HTTP commands directly to REST services.Dubiety
E
13

See if rest-client is of any help.

Edit: Currently I am using Postman - REST Client a google chrome plugin and it's awesome!

Expiatory answered 30/1, 2010 at 0:48 Comment(2)
all browser based extensions will give you a hard time testing your api because it won't allow you to create an independent environment of the hosting browser, so you cant change most of the http headers to test your gzip compression for exampleCrosspollination
–1 for Postman, it hides the fact that server returned 302 and it did another get after that.Tko
C
10

I think REST Assured will suite you very well. It's very easy to send requests and to parse XML and JSON responses. E.g. let's say that a GET request to "/lotto" returns JSON:

{
 "lotto":{
  "lottoId":5,
  "winning-numbers":[2,45,34,23,7,5,3],
  "winners":[{
   "winnerId":23,
   "numbers":[2,45,34,23,3,5]
  },{
   "winnerId":54,
   "numbers":[52,3,12,11,18,22]
  }]
 }
}

You can make the request and validate the response like this:

expect().body("lotto.lottoId", equalTo(5)).when().get("/lotto");
Craniometry answered 25/3, 2011 at 10:11 Comment(0)
H
5

There is also the Jersey Test Framework (http://jersey.java.net/nonav/documentation/latest/user-guide.html#test-framework) but as Johan already mentioned the REST-assured framework I'd also recommend this framework - it has some nice featues like a DSL like syntax, XPath and Schema validation, easy file upload and using Groovy Lambda Expressions to search through returned JSON structures..

I have written two articles..

Hash answered 23/10, 2011 at 19:35 Comment(0)
G
1

Fiddler is a really useful tool, you can create XML based HTTP Requests with a variety of request verbs like GET,POST,PUT,DELETE and so on.

http://www.fiddler2.com/fiddler2/

Gaslight answered 5/4, 2011 at 9:47 Comment(0)
U
0

Maybe Selenium can be of some help, but surely not entirely.

Unseemly answered 30/1, 2010 at 0:44 Comment(1)
Selenium as far as I know has no direct support for JSON and XML, it is good for browser level testing - user interaction testing and even than, not everything works (AJAX). Also one thing I especially don't like about Selenium (don't get me wrong I like using Selenium in overall) is the Selenium IDE, which should help, however it just creates more problems by generating very crappy code (e.g. no JUnit4 support) and behaves differently than the Server (try out the variables without a patch).Powerhouse

© 2022 - 2024 — McMap. All rights reserved.