首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebTestClient未注入

WebTestClient未注入
EN

Stack Overflow用户
提问于 2018-05-17 18:50:28
回答 3查看 11K关注 0票数 5

首先,我是java堆栈的新手,我可能会问一些愚蠢的问题,谢谢您的耐心!

我需要什么:

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

预期:

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

实数:

用我现在的设置

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

如果我使用@WebFluxTest(SomeController.class)ContextConfiguration(...),那么客户机最终将为null。如果我接着添加@TestConfiguration,则会抱怨在冲突中有一些注释,例如,@Configuration

任何想法都是非常感谢的!

代码语言:javascript
复制
@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");
  }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-17 20:37:11

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@WebFluxTest(SomeController.class)
@ContextConfiguration(classes = {
   SomeController.class,
   SomeService.class
 })
 public class SomeControllerTest {
   @Autowired
   private WebTestClient webClient;
   // ...
 }

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

代码语言:javascript
复制
@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");
  }
}
票数 4
EN

Stack Overflow用户

发布于 2020-07-26 00:10:04

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

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

代码语言:javascript
复制
@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"))
        ;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-05-17 19:11:01

该错误表示,当试图在WebTestClient类中自动加载SomeControllerTest bean时,您将得到一个错误,您不能只自动创建必须更改为的WebTestClient

代码语言:javascript
复制
WebTestClient testClient = WebTestClient
  .bindToServer()
  .baseUrl("http://localhost:8080")
  .build();

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50398616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档