LoginSignup
2

More than 3 years have passed since last update.

Mockkであるクラスのstaticなメソッドをmockしたい場合

Posted at

あるFooクラスのstaticなメソッドをmockしたい場合、Mockk1.8.1以降ではmockkStaticを使用する。

// Fooクラス
class Foo {
    @JvmStatic
    fun bar(): Boolean {
        return false
    }
}

Fooクラスのbarメソッドをmockしたい場合次のようにする。

// テストコード
@RunWith(AndroidJUnit4::class)
class FooBarTest {

    @Before
    fun init() {
        mockkStatic(Foo::class)
    }

    @Test
    fun test() {
        every { Foo.bar() } returns true
        .
        .
        .
    }
}

参考

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
2