5

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.

zambotn
  • 735
  • 1
  • 7
  • 20
  • 1
    This looks correct on the first sight. Do you have a complete but minimal reproducer somewhere? – Ladicek Feb 10 '21 at 15:20
  • not really, I am trying to do that... seems that `quarkus-maven-plugin:1.11.1.Final` also fails to build new projects now, just using `1.11.2.Final` seems working but is another version... i will try the old way, reboot & retry – zambotn Feb 10 '21 at 16:17
  • Can you take a look at the application startup logs (you still get those even with tests) and see if there's any stacktrace indicating a failure to inject? – kolossus Feb 16 '21 at 23:13

3 Answers3

4

@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.

2

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Werner
  • 196
  • 2
  • 5
0

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;

  ...
}