How to mock Context using Mockito?

一笑奈何 提交于 2019-11-30 14:33:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!