Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito Mocking Android Context PackageManager Exception

I'm starting out with Mockito for Android headless Unit Test. The part I want to test is in the backend that depends on Context. I tried mocking the Context but I get null when I run the test.

The mocked Context seen in this example doesn't show me how it is mocked: https://developer.android.com/training/testing/unit-testing/local-unit-tests.html#setup

The example from mentioned in link above (https://github.com/googlesamples/android-testing/tree/master/unit/BasicSample) has no example of how the context is mocked.

So I'm a little lost.

I have the following in my gradle dependencies:

testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support:support-annotations:23.0.1'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile('com.android.support.test:testing-support-lib:0.+')

Snippet of code:

@RunWith(MockitoJUnitRunner.class)
public static Test {
  @Mock Context mContext;
  RequestQueue mQueue;

@Test public void getCategories(){
final String SERVER = "http://{...}/categories";
mContext = mock(Context.class);
int size = 20;
when(mContext.getResources().getInteger(R.integer.cache_size)).thenReturn(size);
mQueue = VolleyUtil.newRequestQueue(mContext, null, size);

final Response.Listener<String> listener = new Response.Listener(){
//...
}

Response.ErrorListener error = new Response.ErrorListener(){
///...
}

mQueue.add(new JsonRequest(SERVER, listener, error);
}

VolleyUtil.newRequestQueue(final Context context, HttpStack stack, final int cacheMB){
final File cacheDir = new File(context.getCacheDir(), "volley");
  if(stack == null) {
     if(Build.VERSION.SDK_INT >= 9) {
        stack = new HurlStack();
     } else {
        String userAgent = "volley/0";

        try {
           final String network = context.getPackageName();
           final PackageInfo queue = context.getPackageManager().getPackageInfo(network, 0);
           userAgent = network + "/" + queue.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
           e.printStacktrace();
        }

        stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
     }
  }

  final BasicNetwork network = new BasicNetwork((HttpStack)stack);
  final RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir,diskCacheMB*1000000), network);
  queue.start();
  return queue;

}

My null exception happens at:

final PackageInfo queue = context.getPackageManager().getPackageInfo(network, 0);

Am I suppose to mock PackageManager or the Application instance?

like image 880
user5378148 Avatar asked Sep 30 '15 17:09

user5378148


1 Answers

Since you're not testing the android resources on JUnit, you need to mock this too. for instance:

PackageInfo pi = new PackgeInfo();
// modify it's value the way you desire
when(mContext.getPackageManager().getPackageInfo(network, 0)).thenReturn(pi);

Then continue the unit testing.

like image 186
Mahdi-Malv Avatar answered Oct 17 '22 04:10

Mahdi-Malv