From 1fae41d3e01d0281794d85b2b9b4cacabb4e3091 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 27 Jul 2020 22:14:13 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Rewrite=20test=20without=20using=20m?= =?utf8?q?ocks?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/IdentityManagerTest.kt | 25 +++++++++++----------- .../sone/freenet/wot/WebOfTrustConnectorTest.kt | 18 ++++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.kt index 3768efd..90e010b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.kt @@ -1,33 +1,32 @@ package net.pterodactylus.sone.freenet.wot -import com.google.common.eventbus.* -import net.pterodactylus.sone.freenet.plugin.* -import net.pterodactylus.sone.test.* -import org.hamcrest.MatcherAssert.* -import org.hamcrest.Matchers.* -import org.junit.* -import org.mockito.Mockito.* +import com.google.common.eventbus.EventBus +import net.pterodactylus.sone.freenet.plugin.PluginException +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test /** * Unit test for [IdentityManagerImpl]. */ class IdentityManagerTest { - private val eventBus = mock() - private val webOfTrustConnector = mock() - private val identityManager = IdentityManagerImpl(eventBus, webOfTrustConnector, IdentityLoader(webOfTrustConnector, Context("Test"))) + private val eventBus = EventBus() @Test fun identityManagerPingsWotConnector() { + var pingCalled = false + val webOfTrustConnector = dummyWebOfTrustConnector.overridePing { Unit.also { pingCalled = true } } + val identityManager = IdentityManagerImpl(eventBus, webOfTrustConnector, IdentityLoader(webOfTrustConnector, Context("Test"))) assertThat(identityManager.isConnected, equalTo(true)) - verify(webOfTrustConnector).ping() + assertThat(pingCalled, equalTo(true)) } @Test fun disconnectedWotConnectorIsRecognized() { - doThrow(PluginException::class.java).whenever(webOfTrustConnector).ping() + val webOfTrustConnector = dummyWebOfTrustConnector.overridePing { throw PluginException() } + val identityManager = IdentityManagerImpl(eventBus, webOfTrustConnector, IdentityLoader(webOfTrustConnector, Context("Test"))) assertThat(identityManager.isConnected, equalTo(false)) - verify(webOfTrustConnector).ping() } } diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt new file mode 100644 index 0000000..bf7b830 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt @@ -0,0 +1,18 @@ +package net.pterodactylus.sone.freenet.wot + +val dummyWebOfTrustConnector = object : WebOfTrustConnector { + override fun loadAllOwnIdentities(): Set = emptySet() + override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set = emptySet() + override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?): Set = emptySet() + override fun addContext(ownIdentity: OwnIdentity, context: String) = Unit + override fun removeContext(ownIdentity: OwnIdentity, context: String) = Unit + override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) = Unit + override fun removeProperty(ownIdentity: OwnIdentity, name: String) = Unit + override fun ping() = Unit +} + +open class DelegatingWebOfTrustConnector(private val delegate: WebOfTrustConnector) : WebOfTrustConnector by delegate + +fun WebOfTrustConnector.overridePing(override: () -> Unit): WebOfTrustConnector = object : DelegatingWebOfTrustConnector(this) { + override fun ping() = override() +} -- 2.7.4