Last active
March 12, 2020 04:04
-
-
Save SDiamante13/d941bb61d8d2dd5324bd1aa6f6dbafa0 to your computer and use it in GitHub Desktop.
An example of using Mockito's InOrder method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Test | |
| public void updateCustomer_findsTheCustomerById_andUpdatesTheCustomerInTheDatabase() { | |
| InOrder inOrder = Mockito.inOrder(customerRepository); | |
| Customer customer1 = Customer.builder() | |
| .customerId(1L) | |
| .firstName("Sam") | |
| .lastName("Adams") | |
| .paymentInfo(PaymentInfo.builder().build()) | |
| .build(); | |
| Customer customerToBeUpdated = Customer.builder() | |
| .customerId(1L) | |
| .firstName("Sam") | |
| .lastName("Adams") | |
| .paymentInfo(PaymentInfo.builder() | |
| .cardNumber("4372762") | |
| .expirationDate("7/12") | |
| .securityCode("345") | |
| .zipCode("87262") | |
| .build()) | |
| .build(); | |
| when(customerRepository.findById(anyLong())) | |
| .thenReturn(Optional.of(customer1)); | |
| when(customerRepository.save(any())) | |
| .thenReturn(customerToBeUpdated); | |
| Customer actualUpdatedCustomer = customerService.updateCustomer(customerToBeUpdated); | |
| assertThat(actualUpdatedCustomer).isEqualTo(customerToBeUpdated); | |
| inOrder.verify(customerRepository).save(customerToBeUpdated); | |
| inOrder.verify(customerRepository).findById(1L); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment