在端到端测试中使用 `WebTestClient.mutateWith() ` 时发生 NPE

NPE ocurred when used `WebTestClient.mutateWith() ` in end to end tests

当我为 webflux 应用程序编写一些测试时。我尝试在 WebTestClient 中通过 mutateWith(mockUser().password("password")) 添加凭据,但它导致 NPE 抛出。

我使用 bindToServer 将测试客户端连接到 运行 远程 API,并尝试使用 mutateWith(mockUser().password("password")) 向请求添加基本身份验证。它在测试时抛出 NPE。

更新源代码https://github.com/hantsy/spring-reactive-sample/blob/master/security-method/src/test/java/com/example/demo/IntegrationTests.java#L118-L127

在我探索了 mockUser 的源代码之后,它需要一个 MockServer 环境来 运行 测试,但 运行作为我的示例中的结尾 2。

mutateWith(mockUser().password("password"))更改为.mutate().filter(basicAuthentication("user", "password")).build(),NPE消失了。

希望对您有所帮助。

@Test public void deletingPostsWhenUserCredentialsThenForbidden_mutateWith() throws Exception { this.rest .mutate().filter(basicAuthentication("user", "password")).build() .delete() .uri("/posts/1") .exchange() .expectStatus().is4xxClientError() .expectBody().isEmpty(); }

更新源代码https://github.com/hantsy/spring-reactive-sample/blob/master/security-method/src/test/java/com/example/demo/IntegrationTests.java#L118-L127