Wednesday, September 5, 2012

Spring REST web service Test

We can test the Spring REST web service by following :

1. By java Junit class.
2. By SoapUI.
3. By Firefox browser plugin REST CLIENT.
4. By Chrome browser plugin POST MAN.


1. By java Junit class :
We can write the java junit class to test REST service. its simple test class in ur maven web application.

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.RestTemplate;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
@Test(enabled = false)
public class UserControllerTest {
private static final String REST_SERVICE_URL = "http://localhost:8080/*****/service/users";
@Autowired
private RestTemplate restTemplate;
@BeforeClass
protected void beforeClass() {
restTemplate = new RestTemplate();
}
public void create() {
createAndAssertUser();
}
private User createAndAssertUser() {
User user = new User();
user.setId(10L);
user.setName("utteshkumar");
user.setCompany("$$$$$$$");
user.setEmail("TTTTT@gmail.com");
user.setDesignation("Senior Software Engineer");
return createAndAssertUser(user);
}
private User createAndAssertUser(User user) {
User createdUser = null;
try{
createdUser = restTemplate.postForObject(REST_SERVICE_URL, user, User.class);
}catch (UniquenessViolationException e) {
duplicateRecord(e.getDescription(),user);
}
assertUserNoId(createdUser, user);
return createdUser;
}
@ExceptionHandler
private void duplicateRecord(String msg,User user){
System.out.println("\n user already exist with email : "+user.getEmail());
assertEquals(true,true);
}
private void assertUserNoId(User actual, User expected) {
assertTrue(actual.getId() > 0);
assertEquals(actual.getName(), expected.getName());
}
private void assertUser(User actual, User expected) {
assertTrue(actual.getId() > 0);
assertEquals(actual.getName(), expected.getName());
}
}


2. By SoapUI : We can test by using Open Source SoapUI tool, it very simple configure and test the web services.
http://www.soapui.org/REST-Testing/getting-started.html

3. By Firefox browser plugin REST CLIENT : we can use REST CLIENT plugin
https://addons.mozilla.org/en-US/firefox/addon/restclient/

4. By Chrome browser plugin POST MAN: we can use POST MAN plugin
https://chrome.google.com/webstore/detail/fdmmgilgnpjigdojojpjoooidkmcomcm

Related Posts:

  • RESTful Web Service What Are RESTful Web Services? Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as… Read More
  • Spring REST web service Test We can test the Spring REST web service by following : 1. By java Junit class. 2. By SoapUI. 3. By Firefox browser plugin REST CLIENT. 4. By Chrome browser plugin POST MAN. 1. By java Junit class : We can write the j… Read More

3 comments:

  1. Thank you for sharing this information. The information was very helpful and saved a lot of my time.


    IT Support Los Angeles


    ReplyDelete
  2. Howdy! Did you execute all the options of this website by yourself or you turned to professionals to get some help?

    ReplyDelete