Retrieving dependencies from CDI in tests that implement interface throws java.lang.IllegalStateException if code defined outside JUnit test
See original GitHub issueDescribe the bug
When attempting to inject a dependency that implements an interface, if you use the class rather than the interface it throws a java.lang.IllegalStateException whereas using the interface is fine.
This only seems to be an issue if you try to make a “helper” method that lives outside of a @Test function.
This is also not a problem if you’re injecting a class that doesn’t implement an interface.
Expected behavior You should be able to inject either the interface or a specific implementation, and creating a helper method outside of a JUnit test should be fine for either.
Actual behavior
I have an interface called TestInterface, and an implementation called TestInterfaceImpl and the TestInterfaceImpl is annotated with @ApplicationScoped.
I have a series of tests that inject this dependency in order to call various methods on the dependency, using the context for the rest of the DI. If I attempt to retrieve the dependency within a JUnit test (annotated with @Test) then both of these methods work:
@Test
fun testOne() {
val dep = CDI.current().select(TestInterface::class.java).get()
// Do some stuff - this works
}
@Test
fun testTwo() {
val dep = CDI.current().select(TestInterfaceImpl::class.java).get()
// Do some stuff - this works
}
I have ended up repeating this across all of my tests, so wanted to take this out to a helper function. This sits within the class, but is not part of any @Test function:
private fun getDependency() = CDI.current().select(TestInterface::class.java).get()
@Test
fun testThree() {
val dep = getDependency()
// Do some stuff - this works
}
This above function is fine, but the following does not work and results in the below stack trace.
private fun getDependency() = CDI.current().select(TestInterfaceImpl::class.java).get()
@Test
fun testFour() {
val dep = getDependency()
// This doesn't work :(
}
Stack trace:
Caused by: java.lang.NoSuchMethodError: com.example.TestInterfaceImpl: method <init>()V not found
at com.example.TestInterfaceImpl_ClientProxy.<init>(EngineerSummaryServiceImpl_ClientProxy.zig:21)
at com.example.TestInterfaceImpl_Bean.<init>(EngineerSummaryServiceImpl_Bean.zig:118)
at io.quarkus.arc.setup.Default_ComponentsProvider.addBeans2(Default_ComponentsProvider.zig:1140)
at io.quarkus.arc.setup.Default_ComponentsProvider.getComponents(Default_ComponentsProvider.zig:43)
at io.quarkus.arc.impl.ArcContainerImpl.<init>(ArcContainerImpl.java:102)
at io.quarkus.arc.Arc.initialize(Arc.java:20)
... 82 more
Unable to locate CDIProvider
java.lang.IllegalStateException: Unable to locate CDIProvider
at javax.enterprise.inject.spi.CDI.findAllProviders(CDI.java:121)
at javax.enterprise.inject.spi.CDI.getCDIProvider(CDI.java:82)
at javax.enterprise.inject.spi.CDI.current(CDI.java:64)
...
This is also not a problem if you’re injecting a class that doesn’t implement an interface.
To Reproduce Here’s a reproducer: https://github.com/bnjns/quarkus-bug-7541
Just run
$ ./gradlew clean test
Configuration N/A
Screenshots N/A
Environment (please complete the following information):
- Output of
uname -aorver: Darwin Beans.local 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64 - Output of
java -version: openjdk version “1.8.0_232” - GraalVM version (if different from Java): GraalVM CE 19.2.1 (build 25.232-b07-jvmci-19.2-b03, mixed mode)
- Quarkus version or git rev: 1.2.1.Final
Additional context Using Kotlin 1.3.61, compiled down to Java 1.8 Using Gradle (6.0.1)
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (8 by maintainers)
Top Related StackOverflow Question
Thanks for reporting!
This one looks like a lot of fun 😃 We would definitely need a reproducer for this, so when you get some time, it would be wonderful if you could create one and add it to the description
Yep, we’ve had this problem so many times 😂
I’m happy using a workaround for now, but would be grand if this could get tackled at some point 🙂