android


Does Dagger support dependancy injection for ActivityInstrumentationTestCase2 tests


I am trying to use Dagger in an Android functional test which inherits ActivityInstrumentationTestCase2.
The setup code looks like this:
#Override
protected void setUp() {
// TODO Auto-generated method stub
try {
super.setUp();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectGraph.create(new TestModule()).inject(this);
this.activity = super.getActivity();
}`
The OnCreate method, which is fired by calling super.getActivity(), does not use the classes provided by the TestModule. But if I run my activity manually (outside of the testing context) then all the appropriate classes are provided/injected by my non-test module.
I found a way to use Dagger with ActivityInstrumentationTestCase2 by lazily creating the Object Graph. What I do is wait to create the Object Graph until the very first time a class wants to be injected, so as long as you add your modules before calling getActivity() (which starts the activity lifecycle for the activity under test) and use overrides = true in your test modules, this will work. Here's the relevant classes and snippets:
GraphHolder, as the name implies, holds the ObjectGraph object for us. We will make all calls to this class rather than directly to ObjectGraph.
public class GraphHolder {
private static GraphHolder sInstance;
private Object[] mModules;
private ObjectGraph mGraph;
private GraphHolder() {
}
public static GraphHolder getInstance() {
if (sInstance == null) {
sInstance = new GraphHolder();
}
return sInstance;
}
public void inject(Object object) {
if (mGraph == null) {
create();
}
mGraph.inject(object);
}
public <T> T get(Class<T> type) {
if (mGraph == null) {
create();
}
return mGraph.get(type);
}
public void addModules(Object... modules) {
if (mGraph != null) {
mGraph.plus(modules);
} else {
if (mModules == null) {
mModules = modules;
} else {
mModules = concatenate(mModules, modules);
}
}
}
private void create() {
mGraph = ObjectGraph.create(mModules);
mModules = null;
}
private Object[] concatenate(Object[] a, Object[] b) {
int aLength = a.length;
int bLength = b.length;
Object[] c = new Object[aLength + bLength];
System.arraycopy(a, 0, c, 0, aLength);
System.arraycopy(b, 0, c, aLength, bLength);
return c;
}
}
We'll add our modules in the Application class:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
GraphHolder.getInstance().addModules(getModules());
}
Object[] getModules() {
return new Object[]{
// your modules here
};
}
}
Inside the classes we want to inject, we'll simply call GraphHolder.getInstance().inject(this) rather than ObjectGraph.inject(this)
In our test modules, we'll provide the objects we want to override for testing and add overrides = true to the #Module annotation. This tells the object graph to prefer this module's providers over others if there's a conflict.
Then, in our tests:
#Inject Foo mFoo;
#Override
public void setUp() {
super.setUp();
GraphHolder.getInstance().addModules(new TestFooModule());
GraphHolder.getInstance().inject(this); // This is when the object graph will be created
}
ObjectGraph.create(new TestModule()).inject(this);
This code is trying to inject dependencies created by TestModule into your TestCase instead of the tested Activity. What you'd have to do here is
ObjectGraph.create(new TestModule()).inject(this.activity);

Related Links

Android Wear shows gradients?
How can I dismiss notifications upon firing an intent on Android Wear?
App crashes after making a signed APK?
Android: Get default or currently set alarm sound/tone?
How to inflate a view from a fragment?
Run network task on a separate thread to avoid crash in android api greater than 3
android webview unset default browser
IntelliJ Debugger Gets Completely Lost on Android NullPointerException?
Converting android layout file and it's views into a pdf or image file and saving it
Android Scrollview thumview change
Notifications usually send the same text
Duplicate files during packaging of APK app-debug-unaligned.apk
How to get last known location for Location manager in Android?
Using the 800/900Mhz frequencies on Android device
Eclipse doesn't browse available avds
How to use notifyDataSetChanged in SwipeToFresh

Categories

HOME
windows
eslint
jelastic
soa
alsa
3d-reconstruction
nested-if
hpc
crystal-reports-2013
code-generation
dynatrace
flatpak
mongodb-csharp
altera
azure-powershell
mvc-mini-profiler
foreign-keys
bourbon
guzzle
imagemap
direct3d11
onload
maze
nstimer
winexe
quantlib
errbot
user-defined-functions
vex
alm
boost-asio
dst
zend-debugger
mailkit
activexobject
sql-delete
roblox
recurrent-neural-network
typesafe-config
mongoose-schema
comparison-operators
doc
laravel-elixir
ssrs-2014
congestion-control
drupal-theming
flask-login
vaadin-elements
httrack
gitlist
reindex
java-bytecode-asm
oozie-coordinator
renaming
polyline
choice
rocks
kango-framework
gcloud-ruby
boost-serialization
realsense
piracy-prevention
jnlua
directdraw
java-money
r-package
ons-api
android-launcher
uncompress
haskell-warp
canvg
snoop
typeconverter
git-ftp
zend-currency
java-security
gamekit
dbaccess
cocos3d
wcm
jama
ignite-ui
grails-domain-class
buffering
drupal-nodes
scriptaculous
observium
scmmanager
onload-event
joomla3.1
fsevents
android-holo-everywhere
u2netdk
pylucene
uideviceorientation
office-2007
zen
qtabbar
murmurhash
filesystemobject
sun
unmanagedresources
wcf-proxy

Resources

Encrypt Message