From: David ‘Bombe’ Roden Date: Mon, 27 Jul 2020 20:14:13 +0000 (+0200) Subject: ✅ Rewrite test without using mocks X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1fae41d3e01d0281794d85b2b9b4cacabb4e3091;p=Sone.git ✅ Rewrite test without using mocks --- 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() +}