quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
14.6k stars 2.88k forks source link

`@InjectMock JsonWebToken` fails with ContextNotActiveException #47739

Closed flyinfish closed 1 month ago

flyinfish commented 1 month ago

Describe the bug

since 3.21.4 @InjectMock JsonWebToken jwt fails with ContextNotActiveException, it was running in 3.21.3

org.junit.jupiter.api.extension.TestInstantiationException: Failed to create test instance
        at io.quarkus.test.junit.QuarkusTestExtension.initTestState(QuarkusTestExtension.java:767)
        at io.quarkus.test.junit.QuarkusTestExtension.interceptTestClassConstructor(QuarkusTestExtension.java:733)
        at java.base/java.util.Optional.orElseGet(Optional.java:364)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: jakarta.enterprise.context.ContextNotActiveException: RequestScoped context was not active when trying to obtain a bean instance for a client proxy of PRODUCER_METHOD bean [class=io.quarkus.smallrye.jwt.runtime.auth.JwtPrincipalProducer, id=BxDM8UQSQwnNG8ZE6VKFZ8wrsgg]
        - you can activate the request context for a specific method using the @ActivateRequestContext interceptor binding

Expected behavior

should work imho as @InjectMock should somehow replace the real implementation bound to @RequestScope

Actual behavior

fails with exception above

How to Reproduce?

https://github.com/flyinfish/quarkus--inject-mock-jwt

Workaround

there is a workaround JwtBeanInjectMockWorkaroundTest still running which installs the mock dynamically with QuarkusMock.installMockForType()

    //@InjectMock
    JsonWebToken jwt;

    @BeforeEach
    void workaround() {
        jwt = mock(JsonWebToken.class);
        QuarkusMock.installMockForType(jwt, JsonWebToken.class);
    }

Output of uname -a or ver

Linux C70C7391 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

openjdk 21.0.7 2025-04-15 LTS OpenJDK Runtime Environment Zulu21.42+20-SA (build 21.0.7+6-LTS) OpenJDK 64-Bit Server VM Zulu21.42+20-SA (build 21.0.7+6-LTS, mixed mode, sharing)

Quarkus version or git rev

3.21.4

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937) Maven home: /opt/apache-maven-3.9.9 Java version: 21.0.7, vendor: Azul Systems, Inc., runtime: /usr/lib/jvm/mobi-azuljdk21 Default locale: en, platform encoding: UTF-8 OS name: "linux", version: "5.15.167.4-microsoft-standard-wsl2", arch: "amd64", family: "unix"

Additional information

No response

quarkus-bot[bot] commented 1 month ago

/cc @sberyozkin (jwt)

geoand commented 1 month ago

https://github.com/quarkusio/quarkus/pull/47741 fixes the issue

flyinfish commented 1 month ago

thanks a lot guys! :clap: :muscle:

geoand commented 1 month ago

🙏🏽