@classrulesIs@beforeclassOr@afterclassIs an alternative to.
public staticNanatestruleBy annotating the class, it will be called before the test class.
However, it is not called using PowerMockRunner. This seems like a bug. Reference
The following is the kotlin source that declares a ClassRule in a field of type RxImmediateSchedulerRule that implements a public static TestRule, but it doesn't work.
@RunWith(PowerMockRunner::class)
@PrepareForTest(Auth::class)
class LoginViewModelTest {
    companion object {
        @JvmField
        @ClassRule
        val schedulers: RxImmediateSchedulerRule = RxImmediateSchedulerRule()
    }
    @Test
    fun onClickLogin() {
        val mockAuth = PowerMockito.mock(Auth::class.java)
        val target = LoginViewModel(mockAuth)
        target.mail.set("email")
        target.password.set("password")
        val result = Single.just(AuthEntity().apply {
            accessToken = "123456"
            userId = 100
        })
        PowerMockito.`when`(mockAuth.login("email", "password")).thenReturn(result)
        target.onClickLogin().run()
        Mockito.verify(mockAuth).login("email", "password")
    }
}
It's not a fundamental solution, but it works if you stop `@ ClassRule``` and replace it with `@ Rule```.
    @Rule
    val schedulers: RxImmediateSchedulerRule = RxImmediateSchedulerRule()
//    companion object {
//        @JvmField
//        @ClassRule
//        val schedulers: RxImmediateSchedulerRule = RxImmediateSchedulerRule()
//    }
        Recommended Posts