Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example custom test runner crashes #86

Closed
realdadfish opened this issue Dec 3, 2018 · 6 comments · Fixed by #87
Closed

Example custom test runner crashes #86

realdadfish opened this issue Dec 3, 2018 · 6 comments · Fixed by #87

Comments

@realdadfish
Copy link
Contributor

realdadfish commented Dec 3, 2018

The README.md states to do:

public class ExampleTestRunner extends AndroidJUnitRunner {
  @Override
  public void onStart() {
      TestButler.setup(ApplicationProvider.getApplicationContext());
      super.onStart();
  }

  @Override
  public void finish(int resultCode, Bundle results) {
      TestButler.teardown(ApplicationProvider.getApplicationContext());
      super.finish(resultCode, results);
  }
}

however, this crashed for me with androidX's test-core v1.0.0 with this:

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
   at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
   at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)
   at my.custom.AndroidJUnitRunner.onStart(AndroidJUnitRunner.kt:11)
   at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)

so I figured this might be a race here that the registry doesn't know of the instrumentation yet. It is however totally unneccessary to use the registry at all, since the runner itself is the instrumentation that we need, so it is sufficient to do this:

public class ExampleTestRunner extends AndroidJUnitRunner {
  @Override
  public void onStart() {
      TestButler.setup(getTargetContext());
      super.onStart();
  }

  @Override
  public void finish(int resultCode, Bundle results) {
      TestButler.teardown(getTargetContext());
      super.finish(resultCode, results);
  }
}

This properly initialized TestButler for me, i.e. disabled animations, and ran my tests.

@drewhannay
Copy link
Contributor

Thanks for reporting this. Any chance you could send a PR to update the documentation / examples?

@Kisty
Copy link
Contributor

Kisty commented Dec 4, 2018

So you're suggesting using InstrumentationRegistry.getTargetContext() instead of ApplicationProvider.getApplicationContext()?

Can you try using AndroidX test core 1.0.2? I had a similar problem but it went away after updating AndroidX test.

Also, check you have used the new AndroidX test runner artifact. Perhaps that's what's the problem:

From the docs:

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'

@realdadfish
Copy link
Contributor Author

No, ApplicationProvider.getApplicationContext() just redirects to the InstrumentationRegistry, this won't help. I'm suggesting to use the created application context of the runner itself, since AndroidJUnitRunner extends from Instrumentation.

realdadfish added a commit to realdadfish/test-butler that referenced this issue Dec 4, 2018
@realdadfish
Copy link
Contributor Author

In any case, even if it's an issue with a different andoidx.test version, I think it would be safer to just use the context provided by the runner itself and not one of the static accessors. But that's your call :)

@drewhannay
Copy link
Contributor

I checked our code at LinkedIn and it seems like we already were using getTargetContext() from our Instrumentation subclass anyway 😅

I agree this is probably a safer way to go anyway, so I will merge your pull request. Thanks for the contribution!

drewhannay pushed a commit that referenced this issue Dec 5, 2018
@mennatallah
Copy link

I have the same issue when I'm trying to Run my feature file as iam using a cucumber integrated with Espresso for Android UI
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants