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 ...

Get Building a RESTful Web Service with Spring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.