Skip to content

Instantly share code, notes, and snippets.

@SDiamante13
Created March 6, 2020 02:50
Show Gist options
  • Select an option

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

Select an option

Save SDiamante13/391e3271c88e99ea2620021ca8af13cf to your computer and use it in GitHub Desktop.
An example of how to use doReturn When for a spy.
@Test
public void getCustomerById_returnsCorrectCustomer_whenGivenValidId() {
// Arrange
doReturn(new Customer(123, "Sam"))
.when(spyCustomerRepository).findById(123)
// Act
Customer result = customerService.getCustomerById(123);
// Assert
assertThat(result.name).isEqualTo("Sam");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment