Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experimenting TestRule Flow
package com.elyeproj.testrules
import org.junit.After
import org.junit.AfterClass
import org.junit.Before
import org.junit.BeforeClass
import org.junit.ClassRule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
class ExampleUnitTest {
@get:Rule
val ruleFirst = MyRule("@Rule 1")
@get:Rule
val ruleSecond = MyRule("@Rule 2")
@Before
fun setup() {
println("@Before")
}
@Test
@Throws(Exception::class)
fun test1() {
println("@Test 1")
}
@Test
@Throws(Exception::class)
fun test2() {
println("@Test 2")
}
@After
fun tearDown() {
println("@After")
}
companion object {
@BeforeClass
@JvmStatic
fun setupClass() {
println("@BeforeClass")
}
@AfterClass
@JvmStatic
fun tearDownClass() {
println("@AfterClass")
}
@ClassRule
@JvmField
val classFirst = MyRule("@ClassRule 1")
@ClassRule
@JvmField
val classSecond = MyRule("@ClassRule 2")
}
class MyRule(private val name: String) : TestRule {
override fun apply(base: Statement, description: Description?)
= MyStatement(base)
inner class MyStatement(private val base: Statement) : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
println("$name Before TestRule")
try {
base.evaluate()
} finally {
println("$name After TestRule")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.