✅ Rewrite test without using mocks
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 Jul 2020 20:14:13 +0000 (22:14 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 Jul 2020 20:14:13 +0000 (22:14 +0200)
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerTest.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnectorTest.kt [new file with mode: 0644]

index 3768efd..90e010b 100644 (file)
@@ -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<EventBus>()
-       private val webOfTrustConnector = mock<WebOfTrustConnector>()
-       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 (file)
index 0000000..bf7b830
--- /dev/null
@@ -0,0 +1,18 @@
+package net.pterodactylus.sone.freenet.wot
+
+val dummyWebOfTrustConnector = object : WebOfTrustConnector {
+       override fun loadAllOwnIdentities(): Set<OwnIdentity> = emptySet()
+       override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> = emptySet()
+       override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> = 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()
+}