3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerMockRunnerを使うと@ClassRulesが動かない

Last updated at Posted at 2017-09-10

@ClassRules は、@BeforeClass@AfterClassの代替です。
public staticTestRuleクラスにアノテーションをつけることで、テストクラス前に呼び出されます。

しかし、PowerMockRunnerを使うと呼ばれません。
これはバグのようです。参考

以下は、kotlinのソースで、public staticなTestRuleを実装したRxImmediateSchedulerRuleという型のフィールドにClassRuleを宣言していますが、動作しません。

@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")
    }
}

根本的な解決ではないですが、@ClassRuleをやめて@Ruleにすると動作します。

    @Rule
    val schedulers: RxImmediateSchedulerRule = RxImmediateSchedulerRule()

//    companion object {
//        @JvmField
//        @ClassRule
//        val schedulers: RxImmediateSchedulerRule = RxImmediateSchedulerRule()
//    }
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?