Spring Boot 测试失败并显示“没有名为 'webHandler' 的 bean 可用”

SpringBoot test fails with 'No bean named 'webHandler' available'

我正在尝试 运行 Spring启动 Webflux 测试,但出现错误 No bean named 'webHandler' available 我试图按照 webtestclient

的 Spring 测试文档

违规行似乎在 setup()

public WebTestClient webTestClient;

@Before
public void setUp() {
    webTestClient = WebTestClient.bindToApplicationContext(this.context).build();

我的注释是:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyApplication.class)
@ActiveProfiles("offlineuno")

堆栈跟踪是:

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.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)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

更新:作为解决方法,我正在使用

 webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();

现在我手动 运行 spring 引导服务器(在 IntelliJ 中),然后 运行 测试客户端。

我有一个类似的问题,为了在我的测试中解决它,我做了以下事情:

@WebFluxTest(
        controllers = {
                xxxController.class
        })
public class xxxControllerTest {
    @Autowired
    private WebTestClient webTestClient;

    @MockBean
    private xxxDetailsService xxxDetailsService;

    @Test
    void methodName_shouldReturnWhateverObject_givenCondition() throws Exception {

        String expectedJson = "{...}";

        Mono<xxxDetails> result = Mono.just(...);

        doReturn(result).when(xxxDetailsService).getxxxDetails("value");

        webTestClient.get()
                .uri("/path/{xxxValue}", "value")
                .exchange()
                .expectStatus().isOk()
                .expectHeader().contentType(MediaType.APPLICATION_JSON)
                .expectBody().json(expectedJson);
    }

}

WebTestClient.bindToApplicationContext 用于 Netty

上的应用程序 运行

我猜你的应用程序是 servlet,你应该使用

MockMvcWebTestClient.bindToApplicationContext

参考:

  1. https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#webtestclient-context-config
  2. https://github.com/spring-projects/spring-framework/issues/22544#issuecomment-791494782

添加@WebFluxTest 解决了@aksh1618

评论的问题