How to mock Context using Mockito?

2020-02-26 14:23发布

问题:

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)