From 2debfc43a9fe69e8973b950bf1588218ac5f8df2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 28 Jul 2020 00:34:49 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Rewrite=20test=20to=20not=20use=20mo?= =?utf8?q?cks?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../freenet/wot/IdentityChangeEventSenderTest.kt | 76 +++++++++++++++++----- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt index 0236a3e..31c96d9 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityChangeEventSenderTest.kt @@ -17,19 +17,24 @@ package net.pterodactylus.sone.freenet.wot -import com.google.common.eventbus.* -import net.pterodactylus.sone.freenet.wot.event.* -import net.pterodactylus.sone.test.* -import org.junit.* -import org.mockito.ArgumentMatchers.eq -import org.mockito.Mockito.verify +import com.google.common.eventbus.EventBus +import com.google.common.eventbus.Subscribe +import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent +import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent +import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent +import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent +import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.contains +import org.hamcrest.Matchers.containsInAnyOrder +import org.junit.Test /** * Unit test for [IdentityChangeEventSender]. */ class IdentityChangeEventSenderTest { - private val eventBus = mock() + private val eventBus = EventBus() private val ownIdentities = listOf( createOwnIdentity("O1", setOf("Test"), "KeyA" to "ValueA"), createOwnIdentity("O2", setOf("Test2"), "KeyB" to "ValueB"), @@ -46,16 +51,55 @@ class IdentityChangeEventSenderTest { @Test fun addingAnOwnIdentityIsDetectedAndReportedCorrectly() { val newIdentities = createNewIdentities() + val ownIdentityRemovedEvents = mutableListOf() + eventBus.register(object { + @Subscribe + fun ownIdentityRemoved(ownIdentityRemovedEvent: OwnIdentityRemovedEvent) { + ownIdentityRemovedEvents += ownIdentityRemovedEvent + } + }) + val identityRemovedEvents = mutableListOf() + eventBus.register(object { + @Subscribe + fun identityRemovedEvent(identityRemovedEvent: IdentityRemovedEvent) { + identityRemovedEvents += identityRemovedEvent + } + }) + val owIdentityAddedEvents = mutableListOf() + eventBus.register(object { + @Subscribe + fun ownIdentityAdded(ownIdentityAddedEvent: OwnIdentityAddedEvent) { + owIdentityAddedEvents += ownIdentityAddedEvent + } + }) + val identityAddedEvents = mutableListOf() + eventBus.register(object { + @Subscribe + fun identityAdded(identityAddedEvent: IdentityAddedEvent) { + identityAddedEvents += identityAddedEvent + } + }) + val identityUpdatedEvents = mutableListOf() + eventBus.register(object { + @Subscribe + fun identityUpdatedEvent(identityUpdatedEvent: IdentityUpdatedEvent) { + identityUpdatedEvents += identityUpdatedEvent + } + }) identityChangeEventSender.detectChanges(newIdentities) - verify(eventBus).post(eq(OwnIdentityRemovedEvent(ownIdentities[0]))) - verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[0]))) - verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[0], identities[1]))) - verify(eventBus).post(eq(OwnIdentityAddedEvent(ownIdentities[2]))) - verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[1]))) - verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[2], identities[2]))) - verify(eventBus).post(eq(IdentityRemovedEvent(ownIdentities[1], identities[0]))) - verify(eventBus).post(eq(IdentityAddedEvent(ownIdentities[1], identities[2]))) - verify(eventBus).post(eq(IdentityUpdatedEvent(ownIdentities[1], identities[1]))) + assertThat(ownIdentityRemovedEvents, contains(OwnIdentityRemovedEvent(ownIdentities[0]))) + assertThat(identityRemovedEvents, containsInAnyOrder( + IdentityRemovedEvent(ownIdentities[0], identities[0]), + IdentityRemovedEvent(ownIdentities[0], identities[1]), + IdentityRemovedEvent(ownIdentities[1], identities[0]) + )) + assertThat(owIdentityAddedEvents, contains(OwnIdentityAddedEvent(ownIdentities[2]))) + assertThat(identityAddedEvents, containsInAnyOrder( + IdentityAddedEvent(ownIdentities[2], identities[1]), + IdentityAddedEvent(ownIdentities[2], identities[2]), + IdentityAddedEvent(ownIdentities[1], identities[2]) + )) + assertThat(identityUpdatedEvents, contains(IdentityUpdatedEvent(ownIdentities[1], identities[1]))) } private fun createNewIdentities() = mapOf( -- 2.7.4