Rambo's son
Rambo's son

Reputation: 138

Run one-time, async callback before all junit tests

I would like to call some methods before run all tests. Library provides initializer which preparing device to use:

    SomeLibraryInitializer.init(new SomeLibraryInitializer.Callback() {
        @Override
        public void onFinish() {

        }
    });

The library is ready to use when onFinish is called.

Can't mock library classes (proguard in use), so I have to use that initializer for my JUnit tests.

Is there any way to wait until callback onFinish() is called?

Tried with ShadowApplication.runBackgroundTasks(); and Semaphore, but not seem to work. I'm using @BeforeClass Annotation to run it before all tests.

Upvotes: 1

Views: 337

Answers (1)

Blaz
Blaz

Reputation: 2075

What you want is to extend AndroidJUnitRunner and then set it int build.gradle file like

android {
    defaultConfig {
        testInstrumentationRunner "com.example.MyTestRunner"
    }
}

EDIT

If you look at the source you will see onCreate method that is called once. What you want to do is owerride this method and add your own logic in it. Then when you are done you should call start() metho.

Upvotes: 1

Related Questions