Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to test a REST service in java [closed]

  1. What is the best way to test REST services in Java? What approaches and tool set do developers usually take?

  2. Also if you are writing rest clients that call into third party REST services. What is the best way to test REST clients. Do you have JUnit tests that communicate with a third party REST service. You could run into the risk of service not being available/or production REST service cannot be access without certain credentials.

like image 469
serah Avatar asked Jul 14 '16 18:07

serah


People also ask

What is the best way to test REST API?

If you are not a big fan of command-line tools and rather like a GUI client to test your REST API then Postman is the best tool for you. It comes as a Chrome extension and you can install it on your chrome browser and from thereon. It is probably the most popular tool to test your REST API.


1 Answers

I suggest that you take a look at REST Assured for automated testing of REST services. The following example is copied from it's web page:

For example if your HTTP server returns the following JSON at “http://localhost:8080/lotto/{id}”:

{
   "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 easily use REST Assured to validate interesting things from response:

@Test public void
lotto_resource_returns_200_with_expected_id_and_winners() {

    when().
            get("/lotto/{id}", 5).
    then().
            statusCode(200).
            body("lotto.lottoId", equalTo(5), 
                 "lotto.winners.winnerId", containsOnly(23, 54));

}

See the getting started and usage guides for more information.


If you have implemented your server app using Spring Boot, you may also find the blog post about Integrating Testing a Spring Boot Application that I wrote a couple of years ago interesting. It shows how Spring Boot test support starts an embedded web server and deploys the application to it before executing REST Assured based tests against the REST API. In other words, neither do you have to manually start a web server, nor do you need to re-deploy the app between tests. As a matter of fact, you do not even have to create a .war or .jar file between your code changes in order to validate REST API changes.

like image 112
matsev Avatar answered Oct 04 '22 05:10

matsev