unit testing - Mock Android Application class for tests using Robolectric in App with Dagger 2 -
i'm complete beginner when comes testing , current task fix issue stops existing tests running.
several tests using robolectric
failing error message
java.util.serviceconfigurationerror: org.robolectric.internal.shadowprovider: provider org.robolectric.shadows not subtype @ java.util.serviceloader.fail(serviceloader.java:239) @ java.util.serviceloader.access$300(serviceloader.java:185) @ java.util.serviceloader$lazyiterator.nextservice(serviceloader.java:376) @ java.util.serviceloader$lazyiterator.next(serviceloader.java:404) @ java.util.serviceloader$1.next(serviceloader.java:480)
i read in this github post error can happen if there issues environment configuration. after testing narrow down problem 2 methods in application class
, initpicasso
, initjobdispatcher
, initialize picasso
, firebase
job running ,
public class myapplication extends application { private applicationcomponent applicationcomponent; @override public void oncreate() { super.oncreate(); timber.plant(new timber.debugtree()); androidthreeten.init(this); fabric.with(this, new crashlytics()); initpicasso(); applicationcomponent = daggerapplicationcomponent.builder().applicationmodule(new applicationmodule(this)).build(); initjobdispatcher(); } void initpicasso() { final string picasso_cache = "picasso-cache"; file cachedir = new file(getapplicationcontext().getcachedir(), picasso_cache); if (!cachedir.exists()) { //noinspection resultofmethodcallignored cachedir.mkdirs(); } cache cache = new cache(cachedir, 50 * 1024 * 1024); httplogginginterceptor httplogginginterceptor = new httplogginginterceptor(new httplogginginterceptor.logger() { @override public void log(string message) { timber.d("xyz" + message); } }); httplogginginterceptor.setlevel(httplogginginterceptor.level.headers); okhttpclient client = new okhttpclient.builder() .addnetworkinterceptor(httplogginginterceptor) .cache(cache) .build(); picasso.setsingletoninstance( new picasso.builder(this) .listener(new picasso.listener() { @override public void onimageloadfailed(picasso picasso, uri uri, exception exception) { } }) .downloader(new okhttp3downloader(client)) .build() ); } void initjobdispatcher() { driver mydriver = new googleplaydriver(this); firebasejobdispatcher dispatcher = new firebasejobdispatcher(mydriver); timber.d("setting job at: %s", localdatetime.now().tostring()); job job = dispatcher.newjobbuilder() .setservice(syncjobservice.class) .settag("my-job-tag") .setconstraints(constraint.on_any_network) .settrigger(trigger.executionwindow(15, 25)) .setlifetime(lifetime.until_next_boot) .setrecurring(false) .build(); int result = dispatcher.schedule(job); if (result != firebasejobdispatcher.schedule_result_success) { timber.e("job not scheduled: %d", result); } } public applicationcomponent getapplicationcomponent() { return applicationcomponent; } }
if comment out these 2 methods, tests run fine.
now question is: how can mock application class
or @ least these 2 methods? tried create test application class
overrides these methods, didn't help.
public class myapplicationtest extends application { void initpicasso() { //do nothing here system.out.println("using myapplicationtest application"); } void initjobdispatcher() { //do nothing here } }
or project using dagger2
, there ways using dagger mock application class
? area absolutely new me, advices or suggestions appreciated.
Comments
Post a Comment