question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quarkus CDI-managed beans not available in Pact state callbacks

See original GitHub issue

Problem

We have a Pact REST provider test with state callbacks, where we do not want to use testcontainers to startup the application, but rather a normal @QuarkusTest. For easy use-cases this works, as mentioned in https://github.com/quarkusio/quarkus/issues/9677 and demonstrated in https://github.com/skattela/pact-workshop-jvm-quarkus. But when you need to use @State callbacks to initialize some state in your application, then you’ll find out, that you cannot use CDI-managed beans.

Investigation

I investigated this issue and found out, that with that setup we generate two different test class instances. I think this is normally also the case, but here Pact uses the instance, where Quarkus does not inject anything.

Here’s the first instance creation (which will then be used by Pact) https://github.com/quarkusio/quarkus/blob/2.6/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java#L687 A little below initTestState(extensionContext, state); is called, and it creates the “proper” instance: https://github.com/quarkusio/quarkus/blob/2.6/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java#L727

Possible solutions

Maybe we can return the actualTestInstance within interceptTestClassConstructor(). (see first comment)

Environment (please complete the following information):

  • Output of uname -a or ver: Windows 10 - Version 20H2 (Build 19042.1415)
  • Output of java -version: openjdk version "11.0.10" 2021-01-19
  • GraalVM version (if different from Java): n/a
  • Quarkus version or git rev: 2.5.0-Final
  • Build tool (ie. output of mvnw --version or gradlew --version): Apache Maven 3.8.4

Misc

_Originally posted by @JapuDCret in https://github.com/quarkusio/quarkus/issues/9677#issuecomment-1004754024_

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
EnvyITcommented, Mar 24, 2022

Well, this is still a major issue in my opinion. The fact that it is not possible to use DI or Mockito for state callbacks are a huge disadvantage in comparison to other frameworks.

This workaround is indeed super-hacky and not working when you want to use Mockito proxy classes.

0reactions
holly-cumminscommented, Oct 19, 2022

I’ve semi-duplicated this as https://github.com/quarkiverse/quarkus-pact/issues/2. It may be too hard/irrelevant to fix in main Quarkus, but something that an extension can fix.

I wonder if #27821 is related? Both have to do with undesirable classloaders being used in some parts of JUnit 5 test execution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[FIXED] How to get java web root path in JavaBean without having ...
Since it is found that the application is based on Spring, then the appropriate method to inject a servlet into a Bean is...
Read more >
Why are there more values fuzzed than specified in the dictionary ...
This leads to the tests failing since the format is not supported. ... Quarkus CDI-managed beans not available in Pact state callbacks, 8,...
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