Reputation: 187
I have followed this tutorial to create an e-commerce microservice architecure (in french) and now I am trying to write some tests. My architecture is composed of 4 microservices with Eureka and Zuul:
The payment microservice have to call the orders microservice to check if the order has already been payed or not. And this is what I can't reproduce to write unit tests. I would like to test this microservice without launching the orders microservice.
How can I test it without launching orders microservice?
I already wrote some tests for orders microservice and products microservice.
Here is the payment Controller:
/*
* 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);
}
I am blocked at the step where we retrieve the order corresponding to the payment because it tries to call orders microservice but its not running!
Here is the entire code: https://github.com/kamal951/POC_microservices
Upvotes: 1
Views: 1532
Reputation: 7995
You can easily mock the other microservices you are calling in your unit test. In Mockito (which is bundled in spring-boot-starter-test
), you can do this with the following approach:
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--);
...
}
}
Upvotes: 1