Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDateTime;
- import java.time.Month;
- import org.junit.Before;
- import org.junit.Test;
- import org.mockito.Mockito;
- public class TodoItemServiceTest {
- private static final long ITEM_ID = 3L;
- static class TodoItem {
- private final String description;
- private final LocalDateTime deadline;
- private final long id;
- public TodoItem(long id, String description, LocalDateTime deadline) {
- super();
- this.description = description;
- this.deadline = deadline;
- this.id = id;
- }
- public String getDescription() {
- return description;
- }
- public LocalDateTime getDeadline() {
- return deadline;
- }
- public long getId() {
- return this.id;
- }
- }
- static interface TodoItemStorage {
- // TodoItem createNew(String description, LocalDateTime deadline);
- TodoItem getById(long id) throws UnknownIdException;
- void updateDeadline(long itemId, LocalDateTime newDeadline);
- // List<TodoItem> findWithDeadlineBefore(LocalDateTime latestDeadline);
- // void delete(long id);
- }
- static class UnknownIdException extends Exception {
- public UnknownIdException(long id) {
- super("Unknown id: " + id);
- }
- }
- static class TodoItemService {
- private final TodoItemStorage storage;
- public TodoItemService(TodoItemStorage storage) {
- super();
- this.storage = storage;
- }
- public void delayByOneDay(long itemId) throws UnknownIdException {
- TodoItem item = this.storage.getById(itemId);
- if(null != item) {
- LocalDateTime deadline = item.getDeadline();
- this.storage.updateDeadline(itemId, deadline.plusDays(1));
- }
- }
- }
- TodoItemStorage storage;
- TodoItemService underTest;
- @Before
- public void setUp() {
- this.storage = Mockito.mock(TodoItemStorage.class);
- this.underTest = new TodoItemService(storage);
- }
- @Test
- public void can_delay_by_one_day() throws UnknownIdException {
- // Given:
- LocalDateTime originalDeadline = LocalDateTime.of(2016, Month.MARCH,
- 20, 8, 00);
- // Stub the call to getById to return a known value
- Mockito.when(storage.getById(ITEM_ID)).thenReturn(
- new TodoItem(ITEM_ID, "Blog article", originalDeadline));
- // When:
- this.underTest.delayByOneDay(ITEM_ID);
- // Then:
- // Verify that the storage interface is called with the correct arguments
- Mockito.verify(this.storage).updateDeadline(ITEM_ID,
- LocalDateTime.of(2016, Month.MARCH, 21, 8, 00));
- }
- @Test(expected=UnknownIdException.class)
- public void should_pass_on_unknown_id_exception() throws UnknownIdException {
- // Given:
- Mockito.when(storage.getById(ITEM_ID)).thenThrow(new UnknownIdException(3L));
- // When:
- this.underTest.delayByOneDay(ITEM_ID);
- // Then: expect exception
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement