|
| 1 | +package ch.puzzle.pcts.service.business; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import ch.puzzle.pcts.model.example.Example; |
| 7 | +import ch.puzzle.pcts.service.persistence.ExamplePersistenceService; |
| 8 | +import ch.puzzle.pcts.service.validation.ExampleValidationService; |
| 9 | +import java.util.List; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.mockito.InjectMocks; |
| 13 | +import org.mockito.Mock; |
| 14 | +import org.mockito.MockitoAnnotations; |
| 15 | + |
| 16 | +class ExampleBusinessServiceTest { |
| 17 | + |
| 18 | + @Mock |
| 19 | + private ExampleValidationService validationService; |
| 20 | + |
| 21 | + @Mock |
| 22 | + private ExamplePersistenceService persistenceService; |
| 23 | + |
| 24 | + @InjectMocks |
| 25 | + private ExampleBusinessService businessService; |
| 26 | + |
| 27 | + @BeforeEach |
| 28 | + void setUp() { |
| 29 | + MockitoAnnotations.openMocks(this); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void testGetById() { |
| 34 | + Example example = new Example(1L, "Example1"); |
| 35 | + when(persistenceService.getById(1L)).thenReturn(example); |
| 36 | + |
| 37 | + Example result = businessService.getById(1L); |
| 38 | + |
| 39 | + assertEquals(example, result); |
| 40 | + verify(persistenceService).getById(1L); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testGetAll() { |
| 45 | + List<Example> examples = List.of(new Example(1L, "Example1"), new Example(2L, "Example2")); |
| 46 | + when(persistenceService.getAll()).thenReturn(examples); |
| 47 | + |
| 48 | + List<Example> result = businessService.getAll(); |
| 49 | + |
| 50 | + assertEquals(2, result.size()); |
| 51 | + verify(persistenceService).getAll(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testCreate() { |
| 56 | + Example example = new Example(1L, "Example1"); |
| 57 | + when(persistenceService.create(example)).thenReturn(example); |
| 58 | + |
| 59 | + Example result = businessService.create(example); |
| 60 | + |
| 61 | + assertEquals(example, result); |
| 62 | + verify(validationService).validateOnCreate(example); |
| 63 | + verify(persistenceService).create(example); |
| 64 | + } |
| 65 | +} |
0 commit comments