问题
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)
来源:https://stackoverflow.com/questions/34063848/how-to-mock-context-using-mockito