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 = (RoomDTO) response.getData();
assertNotNull(room);
assertEquals("Room1", room.getName());
}
}
When invoking RoomResource.getRoom()
for an existing room, we expect non-null, successful responses containing a...