Advertisement
Guest User

Mockito TDD Demo

a guest
Jan 13th, 2016
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.71 KB | None | 0 0
  1. import java.time.LocalDateTime;
  2. import java.time.Month;
  3.  
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.mockito.Mockito;
  7.  
  8. public class TodoItemServiceTest {
  9.     private static final long ITEM_ID = 3L;
  10.  
  11.     static class TodoItem {
  12.         private final String description;
  13.         private final LocalDateTime deadline;
  14.         private final long id;
  15.  
  16.         public TodoItem(long id, String description, LocalDateTime deadline) {
  17.             super();
  18.             this.description = description;
  19.             this.deadline = deadline;
  20.             this.id = id;
  21.         }
  22.  
  23.         public String getDescription() {
  24.             return description;
  25.         }
  26.  
  27.         public LocalDateTime getDeadline() {
  28.             return deadline;
  29.         }
  30.  
  31.         public long getId() {
  32.             return this.id;
  33.         }
  34.     }
  35.  
  36.     static interface TodoItemStorage {
  37.         // TodoItem createNew(String description, LocalDateTime deadline);
  38.         TodoItem getById(long id) throws UnknownIdException;
  39.         void updateDeadline(long itemId, LocalDateTime newDeadline);
  40.         // List<TodoItem> findWithDeadlineBefore(LocalDateTime latestDeadline);
  41.         // void delete(long id);
  42.     }
  43.    
  44.     static class UnknownIdException extends Exception {
  45.         public UnknownIdException(long id) {
  46.             super("Unknown id: " + id);
  47.         }
  48.     }
  49.  
  50.     static class TodoItemService {
  51.         private final TodoItemStorage storage;
  52.  
  53.         public TodoItemService(TodoItemStorage storage) {
  54.             super();
  55.             this.storage = storage;
  56.         }
  57.  
  58.         public void delayByOneDay(long itemId) throws UnknownIdException {
  59.             TodoItem item = this.storage.getById(itemId);
  60.             if(null != item) {
  61.                 LocalDateTime deadline = item.getDeadline();
  62.                 this.storage.updateDeadline(itemId, deadline.plusDays(1));
  63.             }
  64.         }
  65.     }
  66.  
  67.     TodoItemStorage storage;
  68.     TodoItemService underTest;
  69.  
  70.     @Before
  71.     public void setUp() {
  72.         this.storage = Mockito.mock(TodoItemStorage.class);
  73.         this.underTest = new TodoItemService(storage);
  74.     }
  75.  
  76.     @Test
  77.     public void can_delay_by_one_day() throws UnknownIdException {
  78.         // Given:
  79.         LocalDateTime originalDeadline = LocalDateTime.of(2016, Month.MARCH,
  80.                 20, 8, 00);
  81.         // Stub the call to getById to return a known value
  82.         Mockito.when(storage.getById(ITEM_ID)).thenReturn(
  83.                 new TodoItem(ITEM_ID, "Blog article", originalDeadline));
  84.  
  85.         // When:
  86.         this.underTest.delayByOneDay(ITEM_ID);
  87.  
  88.         // Then:
  89.         // Verify that the storage interface is called with the correct arguments
  90.         Mockito.verify(this.storage).updateDeadline(ITEM_ID,
  91.                 LocalDateTime.of(2016, Month.MARCH, 21, 8, 00));
  92.     }
  93.    
  94.     @Test(expected=UnknownIdException.class)
  95.     public void should_pass_on_unknown_id_exception() throws UnknownIdException {
  96.         // Given:
  97.         Mockito.when(storage.getById(ITEM_ID)).thenThrow(new UnknownIdException(3L));
  98.  
  99.         // When:
  100.         this.underTest.delayByOneDay(ITEM_ID);
  101.  
  102.         // Then: expect exception
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement