Closed marcoRS closed 8 years ago
Maybe @BeforeClass annotation would be here a solution. It runs a code before any tests
Do you need application context in your tests? If not, then just do gradle jvm tests. On Nov 22, 2015 4:01 PM, "Marco Rodriguez-Suarez" notifications@github.com wrote:
Is there a way to run setup code for tests using AndroidJUnit4.class before Application.onCreate() is called by Instrumentation?
I am using Dagger 2 to inject dependencies in Application.onCreate(). It would be really useful for testing to first be able to set my Components with mock dependencies however I'm finding that Application.onCreate always gets called first before anything else.
I've tried using a static { } block and also @BeforeClass but this doesn't work.
— Reply to this email directly or view it on GitHub https://github.com/googlesamples/android-testing/issues/70.
One thing you can do is create your own ActivityTestRule or override beforeActivityLaunched () http://developer.android.com/reference/android/support/test/rule/ActivityTestRule.html#beforeActivityLaunched() And define the pre operations there.
But if you requeriment is with Dagger2 you can setUp your component with the testDependencies like:
@Before
public void setUp() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
DemoApplication app
= (DemoApplication) instrumentation.getTargetContext().getApplicationContext();
TestComponent component = DaggerMainActivityTest_TestComponent.builder()
.mockClockModule(new MockClockModule())
.build();
app.setComponent(component);
component.inject(this);
}
You have a complete example here: https://github.com/chiuki/android-test-demo and http://blog.sqisland.com/2015/04/dagger-2-espresso-2-mockito.html
I think this is what you're looking for: http://developer.android.com/reference/android/support/test/runner/lifecycle/package-summary.html
Closing as this is not an issue related to the samples. Please use https://groups.google.com/forum/#!forum/android-testing-support-library
Does Espresso allow a way to run setup code for tests using
AndroidJUnit4.class
before Application.onCreate() is called by Instrumentation?I am using Dagger 2 to inject dependencies in Application.onCreate(). It would be really useful for testing to first be able to set my Components with mock dependencies however I'm finding that Application.onCreate always gets called first before anything else.
I've tried using a static { } block and also @BeforeClass annotation but this doesn't work.