Quarkus - @Inject Logger causes NPE in test while using Mockito extension; @QuarkusTest works fine

Viewed 24

org.jboss.logging.Logger with @Inject annotation in my code causes NullPointerException in my unit test annotated with @ExtendWith(MockitoExtension.class), if I define it as a org.mockito.@Mock. It's fine if it's used in @QuarkusTest as @Inject Logger, as Quarkus will inject it.

I don't want to change my unit test to @QuarkusTest only for this reason. How can I avoid this NPE caused by logger not initialized by Mockito?

Is this question related? Mock Quarkus provided logger

1 Answers

I have a workaround now:

  • set logger in src to be package visibility, so it can be accessed from test
  • org.mockito.@Mock it in JUnit test
  • in @BeforeEach, set the mock to the class to test, like someInstance.logger = mockLogger;

It has no impact on the original code base, as Quarkus always requires the injected field to be package-private.

Share Edit Delete Flag

Related