代码之家  ›  专栏  ›  技术社区  ›  Anthony Heaney

Quarkus测试失败:无法调用“io.vertx.ext.web.RoutingContext.get(String)”,因为“context”为null

  •  0
  • Anthony Heaney  · 技术社区  · 2 年前

    我正在与Mockito合作,并试图在JUnit测试中验证Keycloft。

    这在以前是有效的,但在Quarkus 3.2.7.Final更新到3.6.4和Key斗篷从22.0.1更新到23.0.4后停止了。 我要提醒一下,它起了作用,但有时不得不跑两次,因为第一次传球失败了。

    当前错误:

    java.lang.AssertionError:应为项事件,但失败:java.lang.NullPointerException:无法调用“io.vertx.ext.web.RoutingContext.get(String)”,因为“context”为null

    失败发生在awaitItem()步骤

    @QuarkusTest
    class CustomAuthenticationMechanismBasicTest {
    
        @Inject
        CustomAuthenticationMechanism customAuthenticationMechanism;
    
        @Inject
        IdentityProviderManager identityProviderManager;
    
        @Test
        void authenticate() {
            RoutingContext routingContext = Mockito.mock(RoutingContext.class);
            String password = System.getProperty("jdoe-password");
            String b64Auth = Base64.encode(("jdoe:"+password).getBytes(StandardCharsets.UTF_8));
            HeadersMultiMap headers = new HeadersMultiMap();
            headers.add(HttpHeaders.AUTHORIZATION, "Basic "+b64Auth);
            HttpServerRequest httpServerRequest = Mockito.mock(HttpServerRequest.class);
            Mockito.when(routingContext.request()).thenReturn(httpServerRequest);
            Mockito.when(httpServerRequest.headers()).thenReturn(headers);
    
            assertEquals("Basic "+b64Auth, routingContext.request().headers().get(HttpHeaders.AUTHORIZATION));
            UniAssertSubscriber<SecurityIdentity> securityIdentityUniAssertSubscriber =
                    customAuthenticationMechanism.authenticate(routingContext, identityProviderManager)
                    .subscribe().withSubscriber(UniAssertSubscriber.create()).awaitItem();
    

    我为Key斗篷设置了多个租户,用于不同的身份验证授予类型,默认的oidc客户端grant.type=client和一个单独的租户“basic”grant.type=password。

    最后一点,关于CustomAuthenticationMechanism。这是一种自定义身份验证,我正在尝试刷新离线令牌以访问令牌,而无需客户端进行额外的OIDC调用。

    有什么建议吗?

    0 回复  |  直到 2 年前