Skip to content

Commit

Permalink
3.library book checkout tests (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
isen-ng authored Sep 3, 2019
1 parent c15945e commit 16a961e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.5.1</junit.jupiter.version>
<assertj.version>3.13.2</assertj.version>
<mockito.version>1.10.19</mockito.version>
</properties>

<dependencies>
Expand All @@ -29,7 +30,12 @@
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.isenng.libraryunittesting.library;

import com.github.isenng.libraryunittesting.library.duedates.DueDateCalculator;
import com.github.isenng.libraryunittesting.library.exceptions.AlreadyCheckedOutException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.mockito.Mockito.*;

class LibraryBookImplTests {
private LibraryBook libraryBook;
private DueDateCalculator mockDueDateCalculator;

@BeforeEach
void setup() {
mockDueDateCalculator = mock(DueDateCalculator.class);
libraryBook = new LibraryBookImpl(mockDueDateCalculator);
}

@Nested
class Constructor {
@Test
void ShouldThrowIllegalArgumentExceptionIfDueDateCalculatorIsNull() {
// act
Throwable t = catchThrowable(() -> new LibraryBookImpl(null));

// assert
assertThat(t).isInstanceOf(IllegalArgumentException.class);
}
}

@Nested
class CheckOut {
@Test
void ShouldThrowAlreadyCheckedOutExceptionIfBookIsCheckedOut() throws Exception {
// arrange
when(mockDueDateCalculator.calculateDueDate()).thenReturn(LocalDate.now());
libraryBook.checkOut();

// act
Throwable t = catchThrowable(() -> libraryBook.checkOut());

// assert
assertThat(t).isInstanceOf(AlreadyCheckedOutException.class);
}

@Test
void ShouldReturnDueDateIfBookIsNotCheckedOut() throws Exception {
// arrange
LocalDate expected = LocalDate.now();
when(mockDueDateCalculator.calculateDueDate()).thenReturn(expected);

// act
LocalDate result = libraryBook.checkOut();

// assert
assertThat(result).isEqualTo(expected);
verify(mockDueDateCalculator).calculateDueDate();
}
}
}

0 comments on commit 16a961e

Please sign in to comment.