【问题标题】:WebTestClient not injectedWebTestClient 未注入
【发布时间】:2018-10-28 03:29:09
【问题描述】:

首先,我是 Java 堆栈的新手,为了辩护,我可能会问一些愚蠢的问题,感谢您的耐心等待!

我需要什么:

集成测试,但没有发出外部请求。这意味着我必须在堆栈中比普通单元测试更深的地方模拟一个依赖项。 我也不想在上下文中加载所有堆栈。

预期:

只能使用@BeanMock 模拟客户端并通过测试。 (从我的试验来看,这只是在深度模拟第一级)。

实际:

按照我目前的设置

Error creating bean with name 'com.example.demo.SomeControllerTest': Unsatisfied dependency expressed through field 'webClient'

如果我使用 @WebFluxTest(SomeController.class) 和 ContextConfiguration(...) 客户端最终为空。如果我然后添加@TestConfiguration webflux 抱怨有一些注释冲突@Configuration 例如。

非常感谢任何想法!

@RestController
public class SomeController {
  private final SomeService someService;

  @Autowired
  public SomeController(SomeService someService) {
    this.someService = someService;
  }


  @GetMapping(value = "/endpoint")
  public Mono<String> endpoint() {
    return someService.get();
  }
}


@Service
public class SomeService {
  private final Client client;

  @Autowired
  public SomeService(Client client) {
    this.client = client;
  }

  public Mono<String> get() {
    return client.build().get().retrieve().bodyToMono(String.class);
  }
}


@Component
public class Client {
  private final HttpServletRequest request;

  @Autowired
  public Client(HttpServletRequest request) {
    this.request = request;
  }

  public WebClient build() {
    return WebClient.builder()
      .baseUrl("https://httpstat.us/200")
      .build();
  }
}

@RunWith(SpringRunner.class)
@TestConfiguration
@SpringBootTest(classes = {
  SomeController.class,
  SomeService.class
})
@AutoConfigureWebTestClient
public class SomeControllerTest {
  @Autowired
  private WebTestClient webClient;

  @MockBean
  private Client client;


  @Before
  public void setUp() {
    when(client.build())
      .thenReturn(WebClient.create("https://httpstat.us/201"));
  }

  @Test
  public void deepMocking() {
    webClient.get()
      .uri("/endpoint")
      .exchange()
      .expectStatus().isOk()
      .expectBody(String.class).isEqualTo("201 Created");
  }
}

【问题讨论】:

    标签: spring spring-boot mocking integration-testing spring-webflux


    【解决方案1】:

    错误表明您在尝试自动连接时遇到错误 WebTestClient bean 在 SomeControllerTest 类中,您不能只自动装配 WebTestClient 它必须更改为

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

    您可以从本文中找到有关 WebClient 的更多信息 http://www.baeldung.com/spring-5-webclient

    【讨论】:

    • @AutoConfigureWebTestClient 应该根据文档在与@SpringBootTest 而不是@WebFluxTest 一起使用时提供Web 测试客户端。不确定它是如何配置的,但至少 Intellij 将 bean 拾取为在某处定义,很奇怪。
    【解决方案2】:
    @RunWith(SpringRunner.class)
    @WebFluxTest(SomeController.class)
    @ContextConfiguration(classes = {
       SomeController.class,
       SomeService.class
     })
     public class SomeControllerTest {
       @Autowired
       private WebTestClient webClient;
       // ...
     }
    

    这是必需的组合,但我不明白为什么需要将SomeController.class 添加到活动上下文中,@WebFluxTest 不也这样做吗?

    或

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = {
      SomeController.class,
      SomeService.class,
    })
    // @AutoConfigureWebTestClient // does absolutely nothing?
    public class SomeControllerTest {
      @Autowired
      private SomeController controller;
    
      // ...
    
      @Test
      public void deepMocking() {
        WebTestClient.bindToController(controller)
          .build()
          .get()
          .uri("/endpoint")
          .exchange()
          .expectStatus().isOk()
          .expectBody(String.class).isEqualTo("201 Created");
      }
    }
    

    【讨论】:

    • 因为如果你检查WebTestClient的javadoc,你会看到它会用构建方法(buildXxx())实例化自己,这需要一些控制器来逐个测试每个控制器;也许通过自动装配,您只需将此构建器的每个控制器作为 bean 提供,因此您需要它们。
    【解决方案3】:

    经过03天的挣扎,这就是我解决的方法:

    感谢 llooottt:https://www.baeldung.com/spring-5-webclient

    @WebFluxTest
    public class AnimeControllerIntegrTest{
    
        WebTestClient testClient;
    
        @Test
        public void get_RA() {
    
            testClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080/animes").build();
    
            RestAssuredWebTestClient
                    .given()
                    .webTestClient(testClient)
    
                    .when()
                    .get()
    
                    .then()
                    .statusCode(OK.value())
    
                    .body("name" ,hasItem("paulo"))
            ;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 1970-01-01
      • 2020-08-26
      • 2020-03-28
      • 1970-01-01
      • 2012-07-02
      • 2013-10-01
      • 2013-04-14
      • 1970-01-01
      相关资源
      最近更新 更多