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

Instrumentation tests do not run with service registered junit5 extensions #229

Closed
yschimke opened this issue Nov 14, 2020 · 9 comments · Fixed by #232
Closed

Instrumentation tests do not run with service registered junit5 extensions #229

yschimke opened this issue Nov 14, 2020 · 9 comments · Fixed by #232
Assignees
Labels

Comments

@yschimke
Copy link

See this PR

When running existing working JVM tests on Android also. I need to add a compile time dependency on the library providing and registering the extension

https://github.com/square/okhttp/pull/6420/files#diff-4e370c937fb7a9a796291fe5a460c222a5fa57f5e400aa48c5eef8a7d22d7ec7

Then explicitly refer to it

https://github.com/square/okhttp/pull/6420/files#diff-83a70a0ad7b461e83feaa5a3e9412f76257a3fdde5a6a2f603ab142a13da806a

@yschimke
Copy link
Author

yschimke commented Nov 14, 2020

I might just need to set junit.jupiter.extensions.autodetection.enabled, I'll reopen if something more is required.

@yschimke
Copy link
Author

Looking at https://github.com/mannodermaus/android-junit5/blob/master/plugin/android-junit5/src/main/kotlin/de/mannodermaus/gradle/plugins/junit5/Plugin.kt#L80

I tried without luck

android {
...
    testOptions {
      junitPlatform {
        configurationParameter 'junit.jupiter.extensions.autodetection.enabled', 'true'
      }
    }
  }

@mannodermaus mannodermaus self-assigned this Nov 16, 2020
@mannodermaus
Copy link
Owner

mannodermaus commented Nov 16, 2020

Thanks for bringing this up. The bridge API for running JUnit 5 tests using a JUnit 4-compatible Runner (which is how the instrumentation test support library works currently) may not be able to pick up extensions automatically. The corresponding section in their User Guide doesn't say anything about extensions being unsupported, but that might have been an oversight.

@yschimke
Copy link
Author

Feel free to close if you don't intend to support. It's frustrating but there is a verbose workaround.

@mannodermaus
Copy link
Owner

I'll leave it open for now and will check in with the JUnit Team to sync on the intended support for extensions using the old Runner. Will update here as new info comes in!

@mannodermaus mannodermaus added this to the plugin-1.7.0.0 milestone Dec 18, 2020
@mannodermaus mannodermaus added bug and removed question labels Dec 18, 2020
@mannodermaus
Copy link
Owner

Looking into this again. Since the JUnit 5 RunnerBuilder creates its own discovery request from scratch, it will definitely be possible to send custom configuration parameters to instrumentation tests, too. One annoying part about this is that, until this point, I have not figured out a way to set testInstrumentationRunnerArguments from a Gradle plugin yet - any changes to this map I tried to make won't be reflected in the final run. This means that we may have to introduce a way to explicitly set the configuration parameters to the runner, duplicating the DSL statements used for plain unit tests.

Either way, I'm targeting this for the next release.

@yschimke
Copy link
Author

Is there a change I should make to the discovery request I'm building here? https://github.com/square/okhttp/blob/master/native-image-tests/src/main/kotlin/okhttp3/RunTests.kt#L109-L116

Before or after your next release?

Thanks for your help on this.

@mannodermaus
Copy link
Owner

The change would only affect the on-device tests running with the JUnit4/5 hybrid. Not sure about the environment that your RunTests file executes in, but I don't think anything would be needed there. Let me rephrase the work that'll happen here:

Starting from the next version, you'll be able to apply configuration parameters (including the one for auto-detection of extensions) to the instrumentation test runtime through the existing DSL in the build script. This will allow androidTest code to pick up configuration parameters automatically.

android {
  defaultConfig {
    // This will become possible in 1.7.0.0.
    // The value is a comma-separated list, if you need to set more than one
    testInstrumentationRunnerArgument("configurationParameters", "junit.jupiter.extensions.autodetection.enabled=true")
  }
}

The video below exercises a simple instrumentation test with an Int parameter, resolved by this extension:

class ExampleExtension : ParameterResolver {
  override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Boolean =
      parameterContext.parameter.type == Int::class.java

  override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any =
      1000
}

class ExampleExtensionTest {
  @Test
  fun test(value: Int) {
    assertEquals(1000, value)
  }
}

The test passes once the extension is auto-detected at runtime.

Untitled2 mov


I will provide a 1.7.0.0-SNAPSHOT including this feature within today, so you can try it out for your use case in MockWebServer. I wanted to test it myself but it seems like the mockwebserver3-junit5 artifact isn't available on JCenter yet.

@mannodermaus
Copy link
Owner

This is now released in 1.7.0.0. Please let me know if you have any trouble getting this to work with the new API

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

Successfully merging a pull request may close this issue.

2 participants