Skip to content

Commit

Permalink
Upgrade Mockito and Mockito-Kotlin.
Browse files Browse the repository at this point in the history
* Upgrade test library versions.
* Replace initMocks with JUnit rules.
* Use strict stubs.
* Replace "Mockito.`when`" with "whenever".
* Fix a couple tests that were broken.

Closes Hexworks#384.
  • Loading branch information
nanodeath committed May 1, 2021
1 parent b253f96 commit 650f056
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 82 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/TestLibs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions zircon.core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,7 +65,7 @@ kotlin {
implementation(kotlin("test"))
implementation(kotlin("test-junit"))

implementation(mockitoAll)
implementation(mockitoCore)
implementation(mockitoKotlin)
implementation(assertjCore)
implementation(logbackClassic)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion zircon.jvm.libgdx/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kotlin {

with(TestLibs) {
testImplementation(junit)
testImplementation(mockitoAll)
testImplementation(mockitoCore)
testImplementation(assertjCore)
}
}
Expand Down
2 changes: 1 addition & 1 deletion zircon.jvm.swing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ kotlin {

with(TestLibs) {
testImplementation(junit)
testImplementation(mockitoAll)
testImplementation(mockitoCore)
testImplementation(assertjCore)
}
}
Expand Down

0 comments on commit 650f056

Please sign in to comment.