Skip to content

Instantly share code, notes, and snippets.

@BenoitAverty
Created April 20, 2016 07:55
Show Gist options
  • Save BenoitAverty/f065d1a93b697449159b48c3367daeb8 to your computer and use it in GitHub Desktop.
Save BenoitAverty/f065d1a93b697449159b48c3367daeb8 to your computer and use it in GitHub Desktop.
package com.example.unit.testing.application;
import ...;
public class OrderApplicationTest {
@Mock
CustomerRepository customerRepository;
@Mock
OrderRepository orderRepository;
@Mock
PaymentValidationService paymentValidationService;
@InjectMocks
OrderApplication orderApplication = new OrderApplication();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void placeOrderTest() {
Order o = mock(Order.class);
when(o.calculateAmount()).thenReturn(10.0);
Customer customerMock = mock(Customer.class);
when(customerMock.placeOrder(0L)).thenReturn(o);
when(customerRepository.findOne(0L)).thenReturn(customerMock);
when(paymentValidationService.verifyCreditCardPayment("123", 10.0)).thenReturn(true);
Long result = orderApplication.placeOrder(0L, 0L, "123");
verify(customerRepository).save(customerMock);
verify(orderRepository).save(o);
assertThat(result).isEqualTo(0l);
}
@Test
public void getOrderStatusTest() {
Order o = mock(Order.class);
when(o.getStatus()).thenReturn("IN PROGRESS");
when(orderRepository.findOne(0L)).thenReturn(o);
String result = orderApplication.getOrderStatus(0L);
assertThat(result).isEqualTo("IN PROGRESS");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment