O'Reilly logo

Building a RESTful Web Service with Spring by Ludovic Dewailly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mocking

We have built our sample property management system by decoupling the RESTful layer from the service implementation with the use of Java interfaces. Besides helping structure the code base and preventing tight coupling, this process also benefits unit testing. Indeed, instead of using concrete service implementations when testing the web tier of our application, we can rig mocked implementations in. For instance, consider the following unit test:

public class RoomsResourceTest {
  @Test
  public void testGetRoom() throws Exception {
    RoomsResource resource = new RoomsResource();
    InventoryService inventoryService = ... ApiResponse response = resource.getRoom(1); assertEquals(Status.OK, response.getStatus()); assertNotNull(response); RoomDTO room ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required