Skip to content

Instantly share code, notes, and snippets.

@ValeriusGC
Last active March 5, 2018 03:29
Show Gist options
  • Save ValeriusGC/5af05befb13f7d2987163c672a5d9f9d to your computer and use it in GitHub Desktop.
Save ValeriusGC/5af05befb13f7d2987163c672a5d9f9d to your computer and use it in GitHub Desktop.
Kotlin: This show how to test Event Handlers with JUnit & 'Mockito-kotlin'
package vfx.kotlin
import com.nhaarman.mockito_kotlin.argumentCaptor
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import org.junit.Assert
import org.junit.Test
data class Pizza(val name: String)
object PizzaService {
interface PizzaEvent {
fun pizzaAvailable(pizza: Pizza)
}
var listener: PizzaEvent? = null
fun orderPizza(name: String) {
listener?.pizzaAvailable(Pizza(name))
}
}
class VfxModelTestAgain {
@Test
fun testOrderPizzaWithArgumentMatcher() {
val pizzaName = "Margarita"
val service = PizzaService
val event = mock<PizzaService.PizzaEvent>()
service.listener = event
service.orderPizza(pizzaName)
val captor = argumentCaptor<Pizza>()
verify(event).pizzaAvailable(captor.capture())
val capturedPizza = captor.firstValue
Assert.assertEquals(pizzaName, capturedPizza.name)
}
}
***
{pom.xml}
see dependencies:
...
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nhaarman</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>1.5.0</version>
</dependency>
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment