Add unit test for image accessor
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 15:15:22 +0000 (16:15 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 15:15:22 +0000 (16:15 +0100)
src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.kt [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.kt b/src/test/java/net/pterodactylus/sone/template/ImageAccessorTest.kt
new file mode 100644 (file)
index 0000000..eb692ba
--- /dev/null
@@ -0,0 +1,55 @@
+package net.pterodactylus.sone.template
+
+import net.pterodactylus.sone.data.Album
+import net.pterodactylus.sone.data.Image
+import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.whenever
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.nullValue
+import org.junit.Before
+import org.junit.Test
+
+/**
+ * Unit test for [ImageAccessor].
+ */
+class ImageAccessorTest {
+
+       private val accessor = ImageAccessor()
+       private val album = mock<Album>()
+       private val images = listOf(mock<Image>(), mock<Image>())
+
+       @Before
+       fun setupImages() {
+               whenever(album.images).thenReturn(images)
+               images.forEach {
+                       whenever(it.album).thenReturn(album)
+               }
+       }
+
+       @Test
+       fun `accessor returns next image for first image`() {
+               assertThat(accessor.get(null, images[0], "next"), equalTo<Any>(images[1]))
+       }
+
+       @Test
+       fun `accessor returns null for next image of second image`() {
+               assertThat(accessor.get(null, images[1], "next"), nullValue())
+       }
+
+       @Test
+       fun `accessor returns previous image for second image`() {
+               assertThat(accessor.get(null, images[1], "previous"), equalTo<Any>(images[0]))
+       }
+
+       @Test
+       fun `accessor returns null for previous image of first image`() {
+               assertThat(accessor.get(null, images[0], "previous"), nullValue())
+       }
+
+       @Test
+       fun `accessor uses reflection accessor for all other members`() {
+               assertThat(accessor.get(null, images[0], "hashCode"), equalTo<Any>(images[0].hashCode()))
+       }
+
+}