Git Product home page Git Product logo

Comments (5)

piotrek1543 avatar piotrek1543 commented on June 1, 2024

Maybe @BeforeClass annotation would be here a solution. It runs a code before any tests

from testing-samples.

IgorGanapolsky avatar IgorGanapolsky commented on June 1, 2024

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" [email protected]
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.

from testing-samples.

cdsap avatar cdsap commented on June 1, 2024

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

from testing-samples.

nkoroste avatar nkoroste commented on June 1, 2024

I think this is what you're looking for:
http://developer.android.com/reference/android/support/test/runner/lifecycle/package-summary.html

from testing-samples.

JoseAlcerreca avatar JoseAlcerreca commented on June 1, 2024

Closing as this is not an issue related to the samples. Please use https://groups.google.com/forum/#!forum/android-testing-support-library

from testing-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.