From 069bcf304238fb08fe7a5d5ed1d48d7d8e73ebc5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 18 Jun 2017 15:35:47 +0200 Subject: [PATCH] Add Kotlin version of web page test base, convert first test --- .../net/pterodactylus/sone/test/Mockotlin.kt | 3 + .../sone/web/pages/ViewSonePageTest.kt | 5 +- .../pterodactylus/sone/web/pages/WebPageTest2.kt | 102 +++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt index dcd139b..d3e8dad 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt @@ -3,6 +3,7 @@ package net.pterodactylus.sone.test import com.google.inject.Module import org.mockito.ArgumentCaptor import org.mockito.Mockito +import org.mockito.invocation.InvocationOnMock import org.mockito.stubbing.OngoingStubbing inline fun mock(): T = Mockito.mock(T::class.java)!! @@ -22,3 +23,5 @@ inline fun bindMock(): Module = inline fun whenever(methodCall: T) = Mockito.`when`(methodCall)!! inline fun OngoingStubbing.thenReturnMock(): OngoingStubbing = this.thenReturn(mock()) + +operator fun InvocationOnMock.get(index: Int): T = getArgument(index) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt index 5b3c959..7df6b7e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/ViewSonePageTest.kt @@ -19,13 +19,12 @@ import org.junit.Test /** * Unit test for [ViewSonePage]. */ -class ViewSonePageTest: WebPageTest() { +class ViewSonePageTest: WebPageTest2(::ViewSonePage) { init { whenever(currentSone.id).thenReturn("sone-id") } - private val page = ViewSonePage(template, webInterface) private val post1 = createPost("post1", "First Post.", 1000, currentSone) private val post2 = createPost("post2", "Second Post.", 2000, currentSone) private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock()) @@ -34,8 +33,6 @@ class ViewSonePageTest: WebPageTest() { private val directed1 = createPost("post3", "First directed.", 1500, mock(), recipient = currentSone) private val directed2 = createPost("post4", "Second directed.", 2500, mock(), recipient = currentSone) - override fun getPage() = page - @Before fun setup() { whenever(currentSone.posts).thenReturn(mutableListOf(post2, post1)) diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt new file mode 100644 index 0000000..6981f9f --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt @@ -0,0 +1,102 @@ +package net.pterodactylus.sone.web.pages + +import com.google.common.eventbus.EventBus +import freenet.clients.http.ToadletContext +import freenet.support.api.HTTPRequest +import net.pterodactylus.sone.core.Preferences +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.deepMock +import net.pterodactylus.sone.test.get +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.utils.asOptional +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext +import net.pterodactylus.util.web.Method.GET +import org.junit.Before +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.ArgumentMatchers.anyLong +import org.mockito.ArgumentMatchers.anyString + +/** + * Base class for web page tests. + */ +abstract class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePage) { + + protected val currentSone = mock() + private val template = mock