Skip to content

Instantly share code, notes, and snippets.

@ValeriusGC
Created March 5, 2018 04:53
Show Gist options
  • Save ValeriusGC/0be7e9627b3d63f9f5ce27c85affcf96 to your computer and use it in GitHub Desktop.
Save ValeriusGC/0be7e9627b3d63f9f5ce27c85affcf96 to your computer and use it in GitHub Desktop.
Kotlin: This is how one can use parameterized tests along with Mockito-kotlin to provide cyclic tests of Event Handlers
package com.gdetotut.login
import com.nhaarman.mockito_kotlin.argumentCaptor
import com.nhaarman.mockito_kotlin.eq
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.util.Arrays
import org.junit.Assert.assertEquals
/**
* This is cyclic test with parameters. So we use parameterized concept.
*/
@RunWith(Parameterized::class)
class VfxSetUserTest(private val index: Int, private val state: VfxUser.State) {
/**
* We set various parameter for [VfxModel.setUser] and await parameterized result.
*/
@Test
fun testModel() {
val service = VfxModel
val event = com.nhaarman.mockito_kotlin.mock<VfxModel.OnEventListener>()
service.listener = event
service.setUser(index)
val captor = argumentCaptor<VfxUser>()
com.nhaarman.mockito_kotlin.verify(event).onUserChanged(captor.capture(), eq(""))
val captured = captor.firstValue
assertEquals(state, captured.state)
}
companion object {
@JvmStatic
@Parameterized.Parameters(name = "{index}: param = {0}, res = {1}")
fun data() : Collection<Array<Any>> {
return listOf(
arrayOf(0, VfxUser.State.US_GodMode),
arrayOf(1, VfxUser.State.US_Disp),
arrayOf(2, VfxUser.State.US_Admin),
arrayOf(3, VfxUser.State.US_NotReg),
arrayOf(10, VfxUser.State.US_NotReg),
arrayOf(-1, VfxUser.State.US_NotReg)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment