测试需要调用另一个微服务的Spring Boot微服务

时间:2019-04-03 08:59:53

标签: unit-testing spring-boot

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

  • 此处提供产品列表的产品微服务
  • 将处理订单的订单微服务
  • 将处理付款的支付微服务
  • 客户端UI

付款微服务必须调用订单微服务以检查订单是否已经付款。这是编写单元测试无法复制的内容。我想在不启动订单微服务的情况下测试此微服务。

如何在不启动订单微服务的情况下进行测试?

我已经为订单微服务和产品微服务编写了一些测试。

这是付款控制者:

/*
*  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);

}

在我们检索与付款相对应的订单的步骤中,我被阻止了,因为它试图调用订单微服务,但未运行!

这是完整的代码:https://github.com/kamal951/POC_microservices

1 个答案:

答案 0 :(得分:1)

您可以轻松地模拟正在单元测试中调用的其他微服务。在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--);

        ...
    }
}