Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 测试需要调用另一个微服务的spring boot微服务_Unit Testing_Spring Boot - Fatal编程技术网

Unit testing 测试需要调用另一个微服务的spring boot微服务

Unit testing 测试需要调用另一个微服务的spring boot微服务,unit-testing,spring-boot,Unit Testing,Spring Boot,我随后创建了一个电子商务微服务架构(法语),现在我正在尝试编写一些测试。我的体系结构由4个微服务和Eureka和Zuul组成: 此处提供产品列表的产品微服务 处理订单的订单微服务 将处理付款的付款微服务 客户端用户界面 支付微服务必须调用orders微服务来检查订单是否已支付。这就是我不能复制来编写单元测试的东西。我想在不启动orders microservice的情况下测试此microservice 我如何在不启动orders microservice的情况下测试它 我已经为orders

我随后创建了一个电子商务微服务架构(法语),现在我正在尝试编写一些测试。我的体系结构由4个微服务和Eureka和Zuul组成:

  • 此处提供产品列表的产品微服务
  • 处理订单的订单微服务
  • 将处理付款的付款微服务
  • 客户端用户界面
支付微服务必须调用orders微服务来检查订单是否已支付。这就是我不能复制来编写单元测试的东西。我想在不启动orders microservice的情况下测试此microservice

我如何在不启动orders microservice的情况下测试它

我已经为orders microservice和products microservice编写了一些测试

以下是付款控制员:

/*
*  Operations to save a payment and notify the orders microservice to update the status of the sayed oreder
**/
@PostMapping(value = "/payment")
public ResponseEntity<Payment>  payAnOrder(@RequestBody Payment payment){

    // We verify if the order has been already payed
    System.out.println("We verify if the order has been already payed");
    Payment existingPayment = paymentDao.findByidOrder(payment.getIdOrder());
    if(existingPayment != null) throw new ExistingPaymentException("This order has already been payed!");

    // We save the payment
    System.out.println("We save the payment");
    Payment newPayment = paymentDao.save(payment);

    // if the DAO return null, there was a problem when saving the payment
    System.out.println("if the DAO return null, there was a problem when saving the payment");
    if(newPayment == null) throw new ImpossiblePaymentException("Error, impossible to establish the payment, retry later!");

    // We retrieve the order corresponding to that payment by calling orders microservice
    System.out.println("We retrieve the order corresponding to that payment by calling orders microservice");
    Optional<OrderBean> orderReq = microserviceOrderProxy.retrieveOneOrder(payment.getIdOrder());

    // orderReq.get() extract the object of type OrderBean from Optional
    System.out.println("orderReq.get() extract the object of type OrderBean from Optional");
    OrderBean order = orderReq.get();

    // We update the object to mak the order as payed
    System.out.println("We update the object to mak the order as payed");
    order.setOrderPayed(true);

    // We send the object updated to the orders microservice to update the order's status
    System.out.println("We send the object updated to the orders microservice to update the order's status");
    microserviceOrderProxy.updateOrder(order);

    // We return 201 CREATED to notify the client that the payment has been registered
    System.out.println("We return 201 CREATED to notify the client that the payment has been registered");
    return new ResponseEntity<Payment>(newPayment, HttpStatus.CREATED);

}
/*
*保存付款并通知orders microservice更新所述Order状态的操作
**/
@邮购(value=“/payment”)
公共响应支付者(@RequestBody Payment Payment){
//我们核实订单是否已付款
System.out.println(“我们验证订单是否已付款”);
Payment existingPayment=paymentDao.findByidOrder(Payment.getIdOrder());
如果(existingPayment!=null)抛出新的ExistingPaymentException(“此订单已付款!”);
//我们保存付款
System.out.println(“我们保存付款”);
Payment newPayment=paymentDao.save(付款);
//如果DAO返回null,则在保存付款时出现问题
System.out.println(“如果DAO返回null,则在保存付款时出现问题”);
如果(newPayment==null)抛出新的不可能的PaymentException(“错误,无法建立付款,请稍后重试!”);
//我们通过调用orders microservice检索对应于该付款的订单
System.out.println(“我们通过调用orders microservice检索对应于该付款的订单”);
可选orderReq=microserviceOrderProxy.retrieveOneOrder(payment.getIdOrder());
//get()从可选文件中提取OrderBean类型的对象
System.out.println(“orderReq.get()从Optional中提取OrderBean类型的对象”);
OrderBean order=orderReq.get();
//我们更新对象以使订单成为已付款订单
System.out.println(“我们更新对象以使订单成为已付款的”);
order.setOrderPayed(true);
//我们将更新的对象发送到orders microservice以更新订单的状态
System.out.println(“我们将更新的对象发送到orders microservice以更新订单的状态”);
microserviceOrderProxy.updateOrder(订单);
//我们返回201,以通知客户付款已注册
System.out.println(“我们返回201创建以通知客户付款已注册”);
返回新的响应状态(newPayment,HttpStatus.CREATED);
}
我在检索与付款对应的订单的步骤被阻止,因为它试图调用orders microservice,但它没有运行


下面是完整的代码:

您可以轻松地模拟在单元测试中调用的其他微服务。在Mockito(捆绑在
spring boot starter test
中)中,您可以通过以下方法实现这一点:

public class PaymentControllerTest {

    private PaymentController controller;

    @Mock
    private MicroserviceOrderProxy microserviceOrderProxy;

    ... other mocks here

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        controller = new PaymentController(microserviceOrderProxy, ...);
    }

    @Test
    public void exampleTest() {
        Mockito.when(microserviceOrderProxy.updateOrder(Mockito.any())).thenReturn(--mocked result here--);

        ...
    }
}

您可能想了解,它很容易模拟服务调用,但为了进行有效的测试,您需要契约测试。我尝试了这段代码,但仍然失败,因为它尝试调用orders microservice:
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常为false.RetryableException:连接被拒绝:连接执行GEThttp://localhost:9002/orders/2
您需要模拟每个服务调用,这样它就不会变成真正的restTemplate调用。这只是模拟此类服务调用的一个示例,但您需要将此方法应用于相关类中的所有契约。在该示例中,在setUp方法中,他使用microserviceOrderProxy in参数创建PaymentController实例,但控制器没有参数。我不明白它在这里做什么。我建议切换到构造函数注入而不是字段注入。您将能够使用构造函数参数注入mock。如果坚持使用字段注入,则可能需要使用反射注入模拟。