小编MaF*_*FiA的帖子

如何在 WebClient 单元测试中使用 MockWebServer 模拟错误响应

我正在为使用 WebClient 调用 REST 端点的方法编写单元测试。使用 MockWebServer,我能够覆盖成功响应的代码,但我无法找到任何方法来模拟错误响应,以便单元测试中也覆盖与错误处理相关的代码。

源类:

@Service
@Slf4j
public class EmployeeService {

   private final WebClient webClient;
   private final PaymentServiceConfiguration paymentServiceConfiguration;
   
   public EmployeeService(WebClient webClient, PaymentServiceConfiguration paymentServiceConfiguration){
      this.webClient = webClient;
      this.paymentServiceConfiguration= paymentServiceConfiguration;

   }

   public Mono<PaymentDataResponse> getPaymentInfo(PaymentDataRequest paymentDataRequest){
      return webClient
             .post()
             .uri(paymentServiceConfiguration.getBaseUri() + "/payment")
             .body(Mono.just(paymentDataRequest, PaymentDataRequest.class)
             .retrieve()
             .onStatus(HttpStatus::isError, this.handleErrorResponse)
             .bodyToMono(PaymentDataResponse.class)
             .doOnError((throwable)-> {
                 log.error("Exception in getPaymentInfo method. Message {}", throwable.getMessage());
                 throw(Exceptions.propagate(throwable));
             });

   }

   private Mono<? extends Throwable> handleErrorResponse(ClientResponse clientResponse){
      Mono<String> errorMessage = clientResponse.bodyToMono(String.class);
      return errorMessage.flatMap((message) -> {
         log.error("Error response code is: {}, and …
Run Code Online (Sandbox Code Playgroud)

junit mockito spring-webflux spring-webclient

4
推荐指数
1
解决办法
1万
查看次数

在所需的端口上运行springboot应用程序的maven命令是什么

我正在尝试在Windows命令提示符下使用maven命令运行springboot应用程序。我试图在端口8000上运行该应用程序。我尝试了以下命令,但应用程序始终尝试从已被占用的8080开始启动,因此启动失败。

mvn -Dserver.port=8000 spring-boot:run
Run Code Online (Sandbox Code Playgroud)

当我尝试通过IDE运行该应用程序时(将server.port = 8000放置在application.properties文件中之后),该应用程序将在8000上按预期运行。

通过mvn命令运行时为什么不能以“ 8000”开头?命令有问题吗?

maven spring-boot

2
推荐指数
1
解决办法
29
查看次数

在StringBuffer创建时,String对象在哪里存储在内存中?

StringBuffer str = new StringBuffer("Java"); 字符串"Java"将在何处创建?正常堆还是常量池?

接下来如果我修改它as- str.append("九");

修改会在哪里发生?它会修改常量池中的字符串"Java"并将其转换为"Java九"吗?

java

0
推荐指数
1
解决办法
331
查看次数