Book Image

Building a RESTful Web Service with Spring

By : Ludovic Dewailly
Book Image

Building a RESTful Web Service with Spring

By: Ludovic Dewailly

Overview of this book

Table of Contents (17 chapters)
Building a RESTful Web Service with Spring
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Unit testing Spring controllers


Since we declared RESTful endpoints as Java methods with annotations, normal unit testing techniques can be employed. The de facto unit testing library for Java is JUnit. (http://www.junit.org). JUnit is a simple framework used for writing repeatable tests. The following code snippet illustrates how one can test a RESTful endpoint:

public class AvailabilityResourceTest {
  @Test
  public void testGetAvailability() throws Exception {
    AvailabilityService service = ...
    AvailabilityResource resource = new AvailabilityResource(service);
    WebRequest request = ...
    // invalid from date
    ApiResponse response = resource.getAvailability(null, "2017-01-02", "1", request);
    assertEquals(Status.ERROR, response.getStatus());
    assertEquals(17, response.getError().getErrorCode());
    // from is after until
    response = resource.getAvailability("2017-01-03", "2017-01-02", "1", request);
    assertEquals(Status.ERROR, response.getStatus());
    assertEquals...