7
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 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
        .
        .
        .
    }
}

参考

7
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
7
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?