8d279a69a47e49cb6cea91212a9339d9421bcb80
[JavaForFun] /
1 package de.spring.webservices.rest.business;
2
3 import static org.mockito.Mockito.mock;
4 import static org.mockito.Mockito.times;
5 import static org.mockito.Mockito.verify;
6 import static org.mockito.Mockito.when;
7
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.ArgumentCaptor;
14
15 import de.spring.webservices.domain.Car;
16 import de.spring.webservices.rest.client.CarClientService;
17
18 public class BusinessServiceTest {
19
20         private CarClientService carClientService;
21         private BusinessService businessService;
22         
23     @Before
24     public void createTest() {
25         carClientService = mock(CarClientService.class);
26         businessService = new BusinessService(carClientService);        
27     }
28     
29         @Test
30         public void whenDoSomethingWithCarsThenInvokeDoGetCars() {
31                 Car expectedOne = new Car(66L, "test");
32                 Car expectedTwo = new Car(99L, "example");
33                 List<Car> expected = new ArrayList<>();
34                 expected.add(expectedOne);
35                 expected.add(expectedTwo);
36                 when(carClientService.doGetCars()).thenReturn(expected);
37                 
38                 businessService.doSomethingWithCars();
39                 
40                 verify(carClientService, times(1)).doGetCars();
41         }
42     
43         @Test
44         public void whenDoSomethingWithOneCarhenInvokeDoGetCar() {
45                 Long id = 66L;
46                 Car expected = new Car(66L, "test");
47                 
48                 when(carClientService.doGetCar(id)).thenReturn(expected);
49                 
50                 businessService.doSomethingWithCar(id);
51                 
52                 verify(carClientService, times(1)).doGetCar(id);
53         }
54         
55         @Test
56         public void whenCreateNewCarThenCreateNewOne() {
57                 Car expected = new Car(66L, "test");
58                 ArgumentCaptor<Car> argCar = ArgumentCaptor.forClass(Car.class);
59                 
60                 when(carClientService.doNewCar(argCar.capture())).thenReturn(expected);
61                 
62                 businessService.createsNewCar();
63                 
64                 verify(carClientService, times(1)).doNewCar(argCar.getValue());
65         }
66 }