Skip to content

Instantly share code, notes, and snippets.

@SDiamante13
Last active March 12, 2020 04:04
Show Gist options
  • Select an option

  • Save SDiamante13/d941bb61d8d2dd5324bd1aa6f6dbafa0 to your computer and use it in GitHub Desktop.

Select an option

Save SDiamante13/d941bb61d8d2dd5324bd1aa6f6dbafa0 to your computer and use it in GitHub Desktop.
An example of using Mockito's InOrder method.
@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