Unit Testing

The first part of the unit test, is to figure out the REST URL to invoke. We can figure this out, if we look at the CatalogRestController and web.xml. Remember, web.xml maps *.json to the controller. Hence, for all verbs our URL will end with .json.

We used the following information:

Tomcat host: http://localhost:8080
Application Context: /catalog-service 
CatalogRestController mapping: /products

Now, the method getProducts() do not have any value mapping. But, it needs to be a GET call. Hence, for accessing this method, you will use the URL http://localhost:8080/catalog-service/products.json and it needs to be a GET call. You can therefore, just paste the URL in a browser and it will show the JSON with all products.

Similarly, if you look at getProduct(), the request mapping has /{productId}. This is equivalent to the primary key, product_id value in the database. Hence, the URL will be http://localhost:8080/catalog-service/products/1.json , where product_id=1. This will be again a GET verb again, as we are fetching information.

Now, similarly, we can figure out the remaining URL for calling remove(), update() and add(). The below table provide the remaining URL and verbs.

Method

URL

Verb

remove()

http://localhost:8080/catalog-service/products/{productId}.json

DELETE

update()

http://localhost:8080/catalog-service/products/{productId}.json

PUT

add()

http://localhost:8080/catalog-service/products.json

POST

Where {productId} will be replaced by product_id value in the database.

Now, there will be two question:
1. How to invoke DELETE, PUT and POST verb? When we paste the URL in the browser, they all are by default GET requests.
2. How to pass the product information for add() and update()?

For # 1, we can download a browser plugin called as Poster (Just search Poster plugin), which is available for Chrome and Firefox. The below screenshot shows, Poster in action for Firefox.

Poster plugin for Firefox in Action

For # 2, we need to send an input json containing the product information to be insert or updated in the product table. The POST performs and insert, while PUT updates. The json format is exactly as the response received from http://localhost:8080/catalog-service/products/{productId}.json GET verb. However, we have provided an input file by the name POST_PUT_Input.json, which is in the root folder of the project.

In Poster plugin, you need to make sure, that to add a Header by the name "Content-Type" with value "application/json".

For integration testing, we generally run the client code like the mobile application in same network and point to the URLs provided above. However, integration testing is beyond the scope of this article.

This completes the workshop for creating a simple product REST service using Spring MVC 4.

Happy RESTing! 

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.