Retrieving dependencies from CDI in tests that implement interface throws java.lang.IllegalStateException if code defined outside JUnit test

See original GitHub issue

Describe 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 -a or ver: 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:closed
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
geoandcommented, Mar 3, 2020

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

1reaction
bnjnscommented, Mar 4, 2020

The even weirder thing is that a subsequent ./gradlew test (after the first one) passes… Also the tests seem to pass when run from the IDE.

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 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

unit tests which need CDI container result in java.lang ...
I always do this (CDI 2.0 and later): private SeContainer container; @BeforeEach private void ... Then any @Test has access to CDI.
Read more >
Testing Your Application - Quarkus
Learn how to test your Quarkus Application. This guide covers: Testing in JVM mode. Testing in native mode. Injection of resources into tests...
Read more >
Spring Boot, Maven and Eclipse Errors and TroubleShooting ...
Consider defining a bean of type 'CustomerDAO' in your configuration. Error : Detached object passed to persist; Error : java.lang.
Read more >
Integration Testing for Java EE - Oracle
After the configuration of dependencies, you can use Arquillian as the test runner. The unit tests are executed by Arquillian transparently. Maven, Ant,...
Read more >
Weld 5.1.0.Final - CDI Reference Implementation - JBoss.org
If you want to reference a bean in non-Java code that supports ... we may want to use a mock implementation in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found