I have written a package to integrate a Quarkus app with the Linear.app API. On its own the package works fine and all tests are successful. Using Quarkus 3.17.8.
Now I want to use that package in my main Quarkus app. I am getting UnsatisfiedResolutionException
when I inject it so I made a very simple test class and am still getting the exception. When I look in the ArC Beans section of the DEV UI, my package is not mentioned anywhere and it is not in the "removed" section either.
I must be misunderstanding something.
Here is my test class to be injected from the package:
package com.flowt.linear;
import io.quarkus.logging.Log;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Singleton;
@Singleton
// @ApplicationScoped
public class TestActions {
// Constructor
public TestActions() {
Log.info("IssueActions no-args constructor");
}
public String test() {
return "tested";
}
}
The pom in my main app:
<dependency>
<groupId>com.flowt.linear</groupId>
<artifactId>quarkus-linear-integration</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
My test runner:
package com.flowt.central;
import io.quarkus.logging.Log;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import jakarta.inject.Inject;
import com.flowt.linear.TestActions;
@QuarkusTest
public class LinearCDI_Test extends TestUtils {
// The test works if using new:
// TestActions testActions = new TestActions();
// But this doesn't:
@Inject TestActions testActions;
@Test
public void testCDI() {
String testResponse = testActions.test();
Log.info("Action response: " + testResponse);
// Check the response
assertTrue(testResponse == "tested");
}
}
I can make the test work using new
so the class is definitely imported, it is just the CDI is not mapping it.
I also tried injecting into my main app as part of a REST endpoint (ie not in TEST mode) and the injection also fails there. Still no mapping.
Should it work like this or do I need something else for CDI to map my class?
I want the linear package to have @Singleton or @ApplicationScoped because the package creates a graphQL client configured to talk to Linear and exposes methods like linearActions.createIssue(Issue issue)
. I want to be able to use that wrapped client in my app(s).
Thanks, Murray
Your library needs to have one of several ways that mark it as a CDI bean archive for Quarkus to process it as such.
This typically means having it indexed by Jandex or adding a beans.xml
into it.
Here's a link to Quarkus CDI documentation where this is mentioned.
Lastly, sometimes you may not want to index the whole library and just want to add certain class or two as beans/qualifiers etc. This is also possible and you could opt-for CDI build compatible extension model or Quarkus extensions. The latter is more powerful as it allows you to integrate with other Quarkus extensions (not just other CDI bits). Many of these use case and how to achieve them are captured in Quarkus CDI integration guide.