我目前编写了一个我想通过 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)
有谁知道测试设置有什么问题吗?提前谢谢
我正在尝试运行 SpringBoot Webflux 测试,但收到错误No bean named 'webHandler' available
我正在尝试遵循webtestclient的 Spring 测试文档
有问题的行似乎在 setup() 中
public WebTestClient webTestClient;
@Before
public void setUp() {
webTestClient = WebTestClient.bindToApplicationContext(this.context).build();
Run Code Online (Sandbox Code Playgroud)
我的注释是:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyApplication.class)
@ActiveProfiles("offlineuno")
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪是:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'webHandler' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:771)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1111)
at org.springframework.web.server.adapter.WebHttpHandlerBuilder.applicationContext(WebHttpHandlerBuilder.java:157)
at org.springframework.test.web.reactive.server.ApplicationContextSpec.initHttpHandlerBuilder(ApplicationContextSpec.java:43)
at org.springframework.test.web.reactive.server.AbstractMockServerSpec.configureClient(AbstractMockServerSpec.java:86)
at org.springframework.test.web.reactive.server.AbstractMockServerSpec.build(AbstractMockServerSpec.java:107)
at com.cme.clearing.btec.risk.btec.entity.management.web.EntityMgmtControllerTest.setUp(MyControllerTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) …
Run Code Online (Sandbox Code Playgroud) 以下功能:
private Boolean canDoIt(Parameter param) {
return myService
.getMyObjectInReactiveWay(param)
.map(myObject -> myService.checkMyObjectInImperativeWay(myObject))
.block();
}
Run Code Online (Sandbox Code Playgroud)
在运行时工作正常,但是当使用它测试使用它的流程时,WebTestClient
出现以下错误:
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread parallel-1
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) ~[reactor-core-3.4.1.jar:3.4.1]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Assembly trace from producer [reactor.core.publisher.MonoFlatMap] :
reactor.core.publisher.Mono.flatMap
Run Code Online (Sandbox Code Playgroud)
我知道我不应该使用block()
,但我别无选择:该函数必须返回 a Boolean
(而不是 a Mono<Boolean>
)。也许有另一种不使用block()
.
有没有办法让我WebTestClient
不抛出该错误?
使用Reactor Core版本3.4.6
。
java reactive-programming project-reactor spring-webflux webtestclient
我从 Spring Boot 反应式迁移到 mvc。我迁移了控制器,现在我尝试迁移集成测试。
控制器的测试是这样注释的,如果我运行测试它就可以工作。
@RunWith(SpringRunner.class)
@WebFluxTest
public class MyIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
然后我WebFluxTest
像这样替换注释
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)
如果我运行这个测试,我就有了reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8080
。有什么想法如何修复它吗?
我有一个应用程序,其中一个 api 是使用 Get 方法定义的。它还需要请求正文,然后将其映射到 POJO。我正在尝试使用 webTestClient 测试该控制器。但我没有看到使用 get() 方法发送请求正文的选项。不确定我是否以正确的方式定义我的 webTestClient。
我的控制器看起来像:
@GetMapping
public Flux<ResponseBody> getAllResources(@RequestBody Resource resource) {
//related code here...
}
Run Code Online (Sandbox Code Playgroud)
我的测试方法:
@Test
public void myTest() {
webClient.get()
.uri("uri_path")
.header("Content-Type", "application/json")
.accept("application/json")
.exchange()
.expectedStatus.is2xxxSuccessful();
}
Run Code Online (Sandbox Code Playgroud)
我在想,既然控制器允许通过 get 调用将对象绑定到 POJO,那么应该有某种方法使用 webTestClient 来测试它。
java rest integration-testing spring-webclient webtestclient
我正在尝试使用 WebTestClient 检查返回字符串的控制器。但由于某种原因,我收到一个错误。
我使用 Kotlin,所以我尝试将我找到的 Java 示例应用到它,但我不知道如何正确地做到这一点。我错过了什么?
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HelloResourceIT {
@Test
fun shouldReturnGreeting(@Autowired webClient: WebTestClient) {
webClient.get()
.uri("/hello/Foo")
.accept(MediaType.TEXT_PLAIN)
.exchange()
.expectStatus()
.isOk()
.expectBody(String::class.java)
.isEqualTo<Nothing>("Hello Foo!")
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用String
或java.lang.String
而不是Nothing
我收到错误消息时:
类型参数不在其范围内。预期:没有!发现:字符串!
当我使用时,Nothing
我会得到一个 NPE。
WebFlux WebTestClient 和 Kotlin已经存在类型干扰问题,但我使用的是特定类型。字符串在这里似乎不起作用。我缺少什么?