|
1 | 1 | package com.iluwatar.daofactory;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | +import static org.mockito.Mockito.verify; |
| 5 | +import static org.mockito.Mockito.when; |
4 | 6 |
|
| 7 | +import java.util.List; |
| 8 | +import org.bson.types.ObjectId; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
5 | 10 | import org.junit.jupiter.api.Test;
|
6 | 11 |
|
7 | 12 | /** {@link App} */
|
8 | 13 | class AppTest {
|
9 |
| - /** Test ensure that no exception when execute main function */ |
| 14 | + /** Test perform CRUD in main class */ |
| 15 | + private CustomerDAO<Long> mockLongCustomerDAO; |
| 16 | + private CustomerDAO<ObjectId> mockObjectIdCustomerDAO; |
| 17 | + |
| 18 | + @BeforeEach |
| 19 | + void setUp() { |
| 20 | + mockLongCustomerDAO = mock(CustomerDAO.class); |
| 21 | + mockObjectIdCustomerDAO = mock(CustomerDAO.class); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + void testPerformCreateCustomerWithLongId() { |
| 26 | + Customer<Long> c1 = new Customer<>(1L, "Test1"); |
| 27 | + Customer<Long> c2 = new Customer<>(2L, "Test2"); |
| 28 | + |
| 29 | + when(mockLongCustomerDAO.findAll()).thenReturn(List.of(c1, c2)); |
| 30 | + |
| 31 | + App.performCreateCustomer(mockLongCustomerDAO, List.of(c1, c2)); |
| 32 | + |
| 33 | + verify(mockLongCustomerDAO).save(c1); |
| 34 | + verify(mockLongCustomerDAO).save(c2); |
| 35 | + verify(mockLongCustomerDAO).findAll(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void testPerformUpdateCustomerWithObjectId() { |
| 40 | + ObjectId id = new ObjectId(); |
| 41 | + Customer<ObjectId> updatedCustomer = new Customer<>(id, "Updated"); |
| 42 | + |
| 43 | + when(mockObjectIdCustomerDAO.findAll()).thenReturn(List.of(updatedCustomer)); |
| 44 | + |
| 45 | + App.performUpdateCustomer(mockObjectIdCustomerDAO, updatedCustomer); |
| 46 | + |
| 47 | + verify(mockObjectIdCustomerDAO).update(updatedCustomer); |
| 48 | + verify(mockObjectIdCustomerDAO).findAll(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void testPerformDeleteCustomerWithLongId() { |
| 53 | + Long id = 100L; |
| 54 | + Customer<Long> remainingCustomer = new Customer<>(1L, "Remaining"); |
| 55 | + |
| 56 | + when(mockLongCustomerDAO.findAll()).thenReturn(List.of(remainingCustomer)); |
| 57 | + |
| 58 | + App.performDeleteCustomer(mockLongCustomerDAO, id); |
| 59 | + |
| 60 | + verify(mockLongCustomerDAO).delete(id); |
| 61 | + verify(mockLongCustomerDAO).findAll(); |
| 62 | + } |
| 63 | + |
10 | 64 | @Test
|
11 |
| - void shouldExecuteDaoWithoutException() { |
12 |
| - assertDoesNotThrow(() -> App.main(new String[] {})); |
| 65 | + void testDeleteSchema() { |
| 66 | + App.deleteSchema(mockLongCustomerDAO); |
| 67 | + verify(mockLongCustomerDAO).deleteSchema(); |
13 | 68 | }
|
14 | 69 | }
|
0 commit comments