WebTestClient未注入 [英] WebTestClient not injected

查看:144
本文介绍了WebTestClient未注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

First of all I'm new to the java stack and in my defence I might ask something stupid, thank you for your patience!

我需要什么:

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

Integration test but without the external requests being made. That means I have to mock a dependency somewhere deeper in the stack than a normal unit test. I also don't want to load all the stack in the context.

预期:

只能使用@BeanMock模拟客户端并通过测试. (根据我的试验,这仅是对第一级深度的嘲笑.)

Be able to mock only the client with @BeanMock and have the test pass. (From my trials this only mocks the first level in depth).

实际:

通过当前设置,我得到了

With the current setup I'm getting

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

如果我使用@WebFluxTest(SomeController.class)ContextConfiguration(...),则客户端最终将为空.如果再添加@TestConfiguration,则webflux抱怨例如在@Configuration中有一些注释.

If I use @WebFluxTest(SomeController.class) and ContextConfiguration(...) the client ends up being null. If I then add the @TestConfiguration webflux is complaining about having some annotations in conflict @Configuration for example.

任何想法都将不胜感激!

Any ideas are greatly appreciated!

@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");
  }
}

推荐答案

@RunWith(SpringRunner.class)
@WebFluxTest(SomeController.class)
@ContextConfiguration(classes = {
   SomeController.class,
   SomeService.class
 })
 public class SomeControllerTest {
   @Autowired
   private WebTestClient webClient;
   // ...
 }

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

This is the required combo, yet i don't understand why there is the need for adding SomeController.class to the active context, doesn't @WebFluxTest do that as well?

OR

@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未注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆