let*_*raw 11 spring-boot webtestclient
我目前编写了一个我想通过 WebTestClient 进行测试的 put 请求。我遵循了一些教程并根据它调整了我的案例。测试请求会出现错误:
“NOSuchBeanDefinitionException:没有可用的“org.springframework.test.web.reactive.server.WebTestClient”类型的合格 bean:预计至少有 1 个符合自动装配候选资格的 bean。依赖注释:{@org.springframework.beans.factory.annotation .Autowired(必需= true)}”
我查找了一些解决方案,如下所示:Cant autowire `WebTestClient` - no autoconfiguration但无法使其工作,尽管有提示。
这是测试代码:
@SpringBootTest
@AutoConfigureWebTestClient
public class DemonstratorApplicationTests {
private P4uServiceImpl p4uService = new P4uServiceImpl();
@Autowired
WebTestClient webTestClient;
@MockBean
ReqP4uAccount account;
@Test
void testPutAccount(){
ReqP4uAccount request = p4uService.buildRequest("testAccount");
this.webTestClient.put()
.uri("/account")
.contentType(MediaType.APPLICATION_JSON)
.body(request, ReqP4uAccount.class)
.exchange()
.expectStatus().isOk()
.expectBody(P4uAccount.class);
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道测试设置有什么问题吗?提前谢谢
Sus*_*afa 25
以下作品:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class DemoApplicationTests {
@Autowired
WebTestClient webTestClient;
Run Code Online (Sandbox Code Playgroud)
如果删除(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT),将会失败。
如果您查看 WebTestClientAutoConfiguration ,您可以看到它已经存在
@ConditionalOnClass({ WebClient.class, WebTestClient.class }) ,这可能就是它无法工作的原因,除非 Springboot 在期间启动 Web 应用程序上下文(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
/**
* Auto-configuration for {@link WebTestClient}.
*
* @author Stephane Nicoll
* @author Andy Wilkinson
* @since 2.0.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ WebClient.class, WebTestClient.class })
@AutoConfigureAfter({ CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class })
@Import(WebTestClientSecurityConfiguration.class)
@EnableConfigurationProperties
public class WebTestClientAutoConfiguration {
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12189 次 |
| 最近记录: |