How to mock Context using Mockito?

2020-02-26 14:49发布

I'm using Context to access system level services like WifiManager and BluetoothManager. How to mock this getApplicationContext() using Mockito?

3条回答
地球回转人心会变
2楼-- · 2020-02-26 14:58
    Context context = mock(Context.class);
查看更多
别忘想泡老子
3楼-- · 2020-02-26 15:03

If you want to get the context with Kotlin and Mockito, you can do it in the following way:

mock(Context::class.java)
查看更多
\"骚年 ilove
4楼-- · 2020-02-26 15:17

Let's have a look at the following class: MockContext

If you need more insight, check the Official Testing Fundamentals page

查看更多
登录 后发表回答