I'm using Context to access system level services like WifiManager and BluetoothManager. How to mock this getApplicationContext() using Mockito?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Let's have a look at the following class: MockContext
If you need more insight, check the Official Testing Fundamentals page
回答2:
Context context = mock(Context.class);
回答3:
If you want to get the context with Kotlin
and Mockito
, you can do it in the following way:
mock(Context::class.java)