Quarkus does not @Inject beans in @QuarkusTest suite

Viewed 1222

I am pretty new in Quarkus but I'm pretty proficient in other IoC frameworks (Spring). I have a bean declared as follow

@ApplicationScoped
public class TestingRepo {
    public String greet() {
        return "Hi";
    }
}

and I do also have a Quarkus test that looks like this

@QuarkusTest
public class InjectionTest {

    @Inject
    public TestingRepo tr;

    @Test
    public void testInjection() {
        Assertions.assertNotNull(tr);
    }
}

When I do mvn test I got:

[ERROR] testInjection  Time elapsed: 0.006 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: expected: not <null>
     at org.mytest.InjectionTest.testInjection(InjectionTest.java:25)

There is something I'm missing? I'm expecting to have the bean injected and not being null! Thanks for your help.

3 Answers

@QuarkusTest only kicks off with JUnit Jupiter.

You don't have the import lines in your example, but I'd guess you have:

import org.junit.Test;

and changing to

import org.junit.jupiter.api.Test;

will fix your problem.

Had the same problem - and import org.junit.jupiter.api.Test; was already there.

But I noticed that VSCode - for whatever reason - imported

import com.google.inject.Inject;

Changing this to

import javax.inject.Inject;

fixed the problem. The injected Bean was no longer null.

I've had the same issue with Quarkus unit tests. I found a workaround for my current situation that may work here.

I have a config class with several @Produces methods and a unit test which injects one of them as a field. With just @Produces and @Inject, the field remained null. Adding @Named to both the method and injection point fixed it.

@ApplicationScoped
public class MyConfig {
   @Produces
   @Named("mySpecialBean")
   public MySpecialBean mySpecialBean() {
      return new MySpecialBean();
   }
}
@QuarkusTest
public class MyTest {
   @Inject
   @Named("mySpecialBean")
   MySpecialBean mySpecialBean;

  ...
}
Related