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.
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
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Thank you for sharing this information. The information was very helpful and saved a lot of my time.
ReplyDeleteIT Support Los Angeles
Thanks bare bone.
DeleteHowdy! Did you execute all the options of this website by yourself or you turned to professionals to get some help?
ReplyDelete