Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
whole test
package blog.post.blms.proxy
import blog.post.Item
import blog.post.ItemsManager
import blog.post.PaginatedList
import blog.post.blms.Book
import blog.post.blms.Library
import io.mockk.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestFactory
import toItem
import java.util.*
import java.util.Optional.ofNullable
import java.util.UUID.randomUUID
internal class BookDataProviderTest {
private val testLibrary = Library(randomUUID(), "Town library", "1200 Oak St")
private val book1 = Book(randomUUID(), "Foundation and earth", "Sci-Fi")
private val book2 = Book(randomUUID(), "Lord of the rings", "Fantasy")
data class ListBooksTestCase(val description: String,
val libraryId: UUID,
val returnedItems: PaginatedList<Item>,
val expectedBooks: PaginatedList<Book>,
val maxResults: Int? = null,
val nextToken: String? = null)
private val itemsManager: ItemsManager = mockk()
private lateinit var booksDataProvider: BookDataProvider
@BeforeEach
fun setup() {
MockKAnnotations.init(this)
booksDataProvider = BookDataProvider(itemsManager)
}
@Test
fun `successful call to addItem`() {
every { itemsManager.addItem(any(), any()) } just Runs
booksDataProvider.addBookToLibrary(testLibrary, book1)
verify(exactly = 1) {
itemsManager.addItem(testLibrary.identifier, book1.toItem())
}
}
@TestFactory
fun `successful call to listBooks`(): List<DynamicTest> =
listOf(
ListBooksTestCase(
"Getting list of items",
testLibrary.identifier,
itemsList(book1.toItem(), book2.toItem()),
booksList(book1, book2)
),
ListBooksTestCase(
"Getting empty list",
testLibrary.identifier,
itemsList(),
booksList()
)
).map {
it.`successful call to listBooks`()
}
private fun ListBooksTestCase.`successful call to listBooks`(): DynamicTest {
return DynamicTest.dynamicTest(description) {
// Arrange
every {
itemsManager.getItems(testLibrary.identifier, ofNullable(maxResults), ofNullable(nextToken))
} returns (returnedItems)
//Act
val actualBooks = booksDataProvider.listBooks(testLibrary.identifier, maxResults, nextToken)
//Assert
assertEquals(expectedBooks, actualBooks)
}
}
private fun itemsList(vararg items: Item, nextToken: String? = null) =
PaginatedList(items.toMutableList(), Optional.ofNullable(nextToken))
private fun booksList(vararg books: Book, nextToken: String? = null) =
PaginatedList(books.toMutableList(), Optional.ofNullable(nextToken))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment