Created
August 21, 2018 22:58
-
-
Save elye/ebc410b19df5776845e8af1f3b91416d to your computer and use it in GitHub Desktop.
Experimenting TestRule Flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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