diff --git a/buildSrc/src/main/kotlin/TestLibs.kt b/buildSrc/src/main/kotlin/TestLibs.kt index a3ded5693e..98ad4248b6 100644 --- a/buildSrc/src/main/kotlin/TestLibs.kt +++ b/buildSrc/src/main/kotlin/TestLibs.kt @@ -14,8 +14,8 @@ object TestLibs { const val logbackCore = "ch.qos.logback:logback-core:$logbackVersion" const val junit = "junit:junit:$junitVersion" - const val mockitoAll = "org.mockito:mockito-all:$mockitoVersion" - const val mockitoKotlin = "com.nhaarman:mockito-kotlin:$mockitoKotlinVersion" + const val mockitoCore = "org.mockito:mockito-core:$mockitoVersion" + const val mockitoKotlin = "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion" const val assertjCore = "org.assertj:assertj-core:$assertjVersion" const val assertk = "com.willowtreeapps.assertk:assertk-jvm:$assertkVersion" } diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index d77ce5d35c..b212613701 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -13,8 +13,8 @@ object Versions { const val filtersVersion = "2.0.235-1" const val junitVersion = "4.12" - const val mockitoVersion = "1.10.19" - const val mockitoKotlinVersion = "1.6.0" + const val mockitoVersion = "3.9.0" + const val mockitoKotlinVersion = "3.1.0" const val assertjVersion = "3.6.2" const val assertkVersion = "0.23" } diff --git a/zircon.core/build.gradle.kts b/zircon.core/build.gradle.kts index 892750446a..b43e9290a0 100644 --- a/zircon.core/build.gradle.kts +++ b/zircon.core/build.gradle.kts @@ -12,7 +12,7 @@ import TestLibs.assertjCore import TestLibs.kotlinTestAnnotationsCommon import TestLibs.kotlinTestCommon import TestLibs.logbackCore -import TestLibs.mockitoAll +import TestLibs.mockitoCore import TestLibs.mockitoKotlin import org.jetbrains.dokka.gradle.DokkaTask @@ -65,7 +65,7 @@ kotlin { implementation(kotlin("test")) implementation(kotlin("test-junit")) - implementation(mockitoAll) + implementation(mockitoCore) implementation(mockitoKotlin) implementation(assertjCore) implementation(logbackClassic) diff --git a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/api/animation/DefaultAnimationRunnerTest.kt b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/api/animation/DefaultAnimationRunnerTest.kt index d675f87e37..9113541e38 100644 --- a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/api/animation/DefaultAnimationRunnerTest.kt +++ b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/api/animation/DefaultAnimationRunnerTest.kt @@ -1,50 +1,46 @@ package org.hexworks.zircon.api.animation import org.assertj.core.api.Assertions.assertThat +import org.hexworks.cobalt.core.api.UUID import org.hexworks.cobalt.core.platform.factory.UUIDFactory - -import org.hexworks.zircon.api.application.AppConfig import org.hexworks.zircon.api.builder.animation.AnimationBuilder import org.hexworks.zircon.api.builder.grid.TileGridBuilder import org.hexworks.zircon.api.data.Position import org.hexworks.zircon.api.data.Size -import org.hexworks.zircon.internal.animation.impl.DefaultAnimation -import org.hexworks.zircon.internal.animation.impl.DefaultAnimationFrame import org.hexworks.zircon.internal.animation.DefaultAnimationRunner import org.hexworks.zircon.internal.animation.InternalAnimation +import org.hexworks.zircon.internal.animation.impl.DefaultAnimationFrame import org.hexworks.zircon.internal.resource.BuiltInCP437TilesetResource -import org.junit.Before +import org.junit.Rule import org.junit.Test import org.mockito.Mock -import org.mockito.Mockito -import org.mockito.MockitoAnnotations +import org.mockito.junit.MockitoJUnit +import org.mockito.junit.MockitoRule +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever +import org.mockito.quality.Strictness import java.util.concurrent.locks.ReentrantLock @Suppress("UNUSED_VARIABLE") class DefaultAnimationRunnerTest { + @get:Rule + val mockitoRule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS) - private lateinit var target: DefaultAnimationRunner + private val target = DefaultAnimationRunner() @Mock lateinit var animationMock: InternalAnimation - @Before - fun setUp() { - MockitoAnnotations.initMocks(this) - AppConfig.newBuilder().enableBetaFeatures().build() - target = DefaultAnimationRunner() - } - - @Test(expected = IllegalArgumentException::class) + @Test fun shouldCloseProperlyWhenClosed() { + whenever(animationMock.id).thenReturn(UUID.randomUUID()) + whenever(animationMock.isLoopedIndefinitely).thenReturn(false) + + target.start(animationMock) target.close() - target.start(DefaultAnimation( - tick = 1, - loopCount = 1, - totalFrameCount = 1, - uniqueFrameCount = 1, - frames = listOf())) + assertThat(target.closed).isTrue() + verify(animationMock).removeCurrentFrame() } @Test @@ -68,8 +64,8 @@ class DefaultAnimationRunnerTest { val lock = ReentrantLock() val cond = lock.newCondition() - Mockito.`when`(animationMock.id).thenReturn(uuid) - Mockito.`when`(animationMock.isLoopedIndefinitely).thenReturn(false) + whenever(animationMock.id).thenReturn(uuid) + whenever(animationMock.isLoopedIndefinitely).thenReturn(false) val result = target.start(animationMock) @@ -82,14 +78,14 @@ class DefaultAnimationRunnerTest { val uuid = UUIDFactory.randomUUID() val currFrame = DefaultAnimationFrame(Size.one(), listOf(), 1) -// Mockito.`when`(animationMock.id).thenReturn(uuid) -// Mockito.`when`(animationMock.isLoopedIndefinitely).thenReturn(false) -// Mockito.`when`(animationMock.fetchNextFrame()).thenReturn(Maybe.empty()) -// Mockito.`when`(animationMock.fetchCurrentFrame()) +// whenever(animationMock.id).thenReturn(uuid) +// whenever(animationMock.isLoopedIndefinitely).thenReturn(false) +// whenever(animationMock.fetchNextFrame()).thenReturn(Maybe.empty()) +// whenever(animationMock.fetchCurrentFrame()) // .then { // currFrame // } -// Mockito.`when`(animationMock.hasNextFrame()).thenReturn(false) +// whenever(animationMock.hasNextFrame()).thenReturn(false) val tileGrid = TileGridBuilder.newBuilder() .withSize(Size.create(50, 50)) diff --git a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/uievent/impl/UIEventToComponentDispatcherTest.kt b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/uievent/impl/UIEventToComponentDispatcherTest.kt index 03db549d13..1f80547552 100644 --- a/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/uievent/impl/UIEventToComponentDispatcherTest.kt +++ b/zircon.core/src/jvmTest/kotlin/org/hexworks/zircon/internal/uievent/impl/UIEventToComponentDispatcherTest.kt @@ -1,23 +1,25 @@ package org.hexworks.zircon.internal.uievent.impl -import com.nhaarman.mockito_kotlin.anyOrNull -import com.nhaarman.mockito_kotlin.times -import com.nhaarman.mockito_kotlin.verify import org.assertj.core.api.Assertions.assertThat import org.hexworks.zircon.api.uievent.* import org.hexworks.zircon.internal.behavior.ComponentFocusOrderList import org.hexworks.zircon.internal.component.InternalContainer import org.hexworks.zircon.internal.component.impl.RootContainer import org.junit.Before +import org.junit.Rule import org.junit.Test import org.mockito.Mock -import org.mockito.Mockito -import org.mockito.MockitoAnnotations.initMocks +import org.mockito.junit.MockitoJUnit +import org.mockito.junit.MockitoRule +import org.mockito.kotlin.* +import org.mockito.quality.Strictness import kotlin.contracts.ExperimentalContracts @Suppress("TestFunctionName") @ExperimentalContracts class UIEventToComponentDispatcherTest { + @get:Rule + val mockitoRule: MockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS) lateinit var target: UIEventToComponentDispatcher @@ -35,17 +37,16 @@ class UIEventToComponentDispatcherTest { @Before fun setUp() { - initMocks(this) - Mockito.`when`(rootMock.calculatePathTo(anyOrNull())).thenReturn(listOf(rootMock)) + whenever(rootMock.calculatePathTo(anyOrNull())).thenReturn(listOf(rootMock)) target = UIEventToComponentDispatcher(rootMock, focusOrderListMock) } @Test fun dispatchShouldReturnPassWhenThereIsNoTarget() { - Mockito.`when`(focusOrderListMock.focusedComponent).thenReturn(rootMock) + whenever(focusOrderListMock.focusedComponent).thenReturn(rootMock) - Mockito.`when`(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) - Mockito.`when`(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) + whenever(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) + whenever(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) val result = target.dispatch(KEY_A_PRESSED_EVENT) @@ -55,9 +56,9 @@ class UIEventToComponentDispatcherTest { @Test fun dispatchShouldReturnProcessedWhenTargetsDefaultIsRun() { - Mockito.`when`(focusOrderListMock.focusedComponent).thenReturn(rootMock) - Mockito.`when`(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) - Mockito.`when`(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Processed) + whenever(focusOrderListMock.focusedComponent).thenReturn(rootMock) + whenever(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) + whenever(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Processed) val result = target.dispatch(KEY_A_PRESSED_EVENT) @@ -67,70 +68,71 @@ class UIEventToComponentDispatcherTest { @Test fun dispatchShouldReturnPreventDefaultWhenChildPreventedDefault() { - Mockito.`when`(focusOrderListMock.focusedComponent).thenReturn(child1Mock) + whenever(focusOrderListMock.focusedComponent).thenReturn(child1Mock) - Mockito.`when`(rootMock.calculatePathTo(child1Mock)).thenReturn(listOf(rootMock, child0Mock, child1Mock)) + whenever(rootMock.calculatePathTo(child1Mock)).thenReturn(listOf(rootMock, child0Mock, child1Mock)) - Mockito.`when`(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) - Mockito.`when`(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) + whenever(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) + whenever(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) - Mockito.`when`(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(PreventDefault) - Mockito.`when`(child0Mock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)) - .thenThrow(IllegalStateException("Child mock 0 shouldn't have been called with key pressed in the capture phase")) + whenever(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(PreventDefault) - Mockito.`when`(child1Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) - Mockito.`when`(child1Mock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)) - .thenThrow(IllegalStateException("Child mock 1 shouldn't have been called with key pressed in the target phase")) + whenever(child1Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(Pass) - Mockito.`when`(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)).thenReturn(Pass) - Mockito.`when`(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)).thenReturn(Pass) - Mockito.`when`(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)) - .thenThrow(IllegalStateException("Root mock shouldn't have been called with key pressed in the bubble phase")) + whenever(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)).thenReturn(Pass) + whenever(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)).thenReturn(Pass) val result = target.dispatch(KEY_A_PRESSED_EVENT) + // Child mock 0 shouldn't be called with key pressed in the capture phase + verify(child0Mock, never()).keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE) + // Child mock 1 shouldn't be called with key pressed in the target phase + verify(child1Mock, never()).keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET) + // Root mock shouldn't be called with key pressed in the bubble phase + verify(rootMock, never()).keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE) + assertThat(result).isEqualTo(PreventDefault) } @Test fun dispatchShouldReturnStopPropagationWhenFirstChildStoppedPropagation() { - Mockito.`when`(focusOrderListMock.focusedComponent).thenReturn(child1Mock) - Mockito.`when`(rootMock.calculatePathTo(child1Mock)).thenReturn(listOf(rootMock, child0Mock, child1Mock)) - - Mockito.`when`(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) - Mockito.`when`(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) + whenever(focusOrderListMock.focusedComponent).thenReturn(child1Mock) + whenever(rootMock.calculatePathTo(child1Mock)).thenReturn(listOf(rootMock, child0Mock, child1Mock)) - Mockito.`when`(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(StopPropagation) + whenever(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) + whenever(rootMock.keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) - Mockito.`when`(child1Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET)) - .thenThrow(IllegalStateException("Child mock 1 shouldn't have been called with process in the target phase")) - Mockito.`when`(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)) - .thenThrow(IllegalStateException("Child mock 0 shouldn't have been called with process in the bubble phase")) - Mockito.`when`(rootMock.process(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE)) - .thenThrow(IllegalStateException("Root mock shouldn't have been called with process in the bubble phase")) + whenever(child0Mock.process(KEY_A_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(StopPropagation) val result = target.dispatch(KEY_A_PRESSED_EVENT) + // Child mock 1 shouldn't be called with process in the target phase + verify(child1Mock, never()).keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.TARGET) + // Child mock 0 shouldn't be called with process in the bubble phase + verify(child0Mock, never()).keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE) + // Root mock shouldn't be called with process in the bubble phase + verify(rootMock, never()).keyPressed(KEY_A_PRESSED_EVENT, UIEventPhase.BUBBLE) + assertThat(result).isEqualTo(StopPropagation) } @Test fun When_a_child_stops_propagation_of_the_tab_key_Then_component_events_shouldnt_be_performed() { - Mockito.`when`(focusOrderListMock.focusedComponent).thenReturn(child0Mock) - Mockito.`when`(rootMock.calculatePathTo(child0Mock)).thenReturn(listOf(rootMock, child0Mock)) + whenever(focusOrderListMock.focusedComponent).thenReturn(child0Mock) + whenever(rootMock.calculatePathTo(child0Mock)).thenReturn(listOf(rootMock, child0Mock)) - Mockito.`when`(rootMock.process(TAB_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) - Mockito.`when`(rootMock.keyPressed(TAB_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) + whenever(rootMock.process(TAB_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) + whenever(rootMock.keyPressed(TAB_PRESSED_EVENT, UIEventPhase.CAPTURE)).thenReturn(Pass) - Mockito.`when`(child0Mock.process(TAB_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(StopPropagation) + whenever(child0Mock.process(TAB_PRESSED_EVENT, UIEventPhase.TARGET)).thenReturn(StopPropagation) val result = target.dispatch(TAB_PRESSED_EVENT) - verify(child0Mock, times(0)).focusGiven() + verify(child0Mock, never()).focusGiven() assertThat(result).isEqualTo(StopPropagation) } diff --git a/zircon.jvm.libgdx/build.gradle.kts b/zircon.jvm.libgdx/build.gradle.kts index 57a63cc81b..dfdea3c8f2 100644 --- a/zircon.jvm.libgdx/build.gradle.kts +++ b/zircon.jvm.libgdx/build.gradle.kts @@ -40,7 +40,7 @@ kotlin { with(TestLibs) { testImplementation(junit) - testImplementation(mockitoAll) + testImplementation(mockitoCore) testImplementation(assertjCore) } } diff --git a/zircon.jvm.swing/build.gradle.kts b/zircon.jvm.swing/build.gradle.kts index 3ba4dd2e91..63282cfd77 100644 --- a/zircon.jvm.swing/build.gradle.kts +++ b/zircon.jvm.swing/build.gradle.kts @@ -30,7 +30,7 @@ kotlin { with(TestLibs) { testImplementation(junit) - testImplementation(mockitoAll) + testImplementation(mockitoCore) testImplementation(assertjCore) } }