@ClassRules doesn't work with PowerMockRunner

@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

@ClassRules doesn't work with PowerMockRunner
When PyCall doesn't work with PyCall :: PythonNotFound
Things to check when it doesn't work with proguard
The program doesn't work, the program doesn't stop ...
Automate your work with Rake
ruby get.chomp.to_i doesn't work, doesn't load
Arrays.asList (int []). Contains (int) doesn't work
What to do if validation doesn't work with the update action
[Beginner] When rails s doesn't work
Work with Google Sheets from Java
mysql doesn't start up with docker.
With CodeDeploy, Install doesn't end endlessly