Disco S2 Disco S2 - 1 year ago 79
Android Question

Using Different Test Runners For Different Tests

I am using

Dagger 2
and trying to provide different dependencies at test time.

I can provide different dependencies for the tests but unfortunately, these then apply to all tests in the

I do this by specifying a
which uses a
. In my
, which extends my
, I supply the
rather than the real
. Then I have
that extends the
method like so:

public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, TestApplication.class.getName(), context);

Then to use this runner I replace the current runner with the following:

testInstrumentationRunner "com.company.myapp.TestRunner"

My Question

How do I use a different component/application/test runner for different tests rather than use the same for each one? I may want to mock a dependency in one test but not in another?

Answer Source

I have found a solution to this in a blog post. It bypasses creating a custom runner and instead uses a Rule to set a different component within the loaded Application. This means you can supply different components for each test class which is what I wanted to do.

The blog post by the guys at Ribot Labs can be found here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download