unit testing - Mockito Mocking Android Context PackageManager Exception -


i'm starting out mockito android headless unit test. part want test in backend depends on context. tried mocking context null when run test.

the mocked context seen in example doesn't show me how mocked: https://developer.android.com/training/testing/unit-testing/local-unit-tests.html#setup

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

so i'm little lost.

i have following in 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.cass) 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 suppose mock package manager or application instance?


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -