|
| 1 | +import static org.assertj.core.api.Assertions.assertThat; |
| 2 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 3 | + |
| 4 | +import com.codedifferently.lesson15.Employee; |
| 5 | +import com.codedifferently.lesson15.EmployeeManager; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +public class EmployeeManagerTest { |
| 10 | + EmployeeManager employeeManager; |
| 11 | + Employee employee; |
| 12 | + Employee employee2; |
| 13 | + |
| 14 | + @BeforeEach |
| 15 | + void setUp() { |
| 16 | + employeeManager = new EmployeeManager(); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + void testAddEmployee() { |
| 21 | + employee = new Employee(999, "Joseph", "Boxing", 150000000.5); |
| 22 | + employeeManager.addEmployee(employee); |
| 23 | + assertThat(employeeManager.getEmployeeCount()).isEqualTo(1); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void testGetEmployee() { |
| 28 | + employee = new Employee(999, "Joseph", "Boxing", 150000000.5); |
| 29 | + employeeManager.addEmployee(employee); |
| 30 | + assertThat(employeeManager.getEmployee(999)).isEqualTo(employee); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void testGetEmployee_failedDueToNull() throws RuntimeException { |
| 35 | + assertThatThrownBy(() -> employeeManager.getEmployee(9)) |
| 36 | + .isInstanceOf(IllegalArgumentException.class) |
| 37 | + .hasMessage("Employee does not in collection with id 9"); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void testEmployeeUpdate() { |
| 42 | + employee = new Employee(999, "Joseph", "Boxing", 150000000.5); |
| 43 | + employeeManager.addEmployee(employee); |
| 44 | + employee2 = new Employee(99, "Angelica", "CodeDifferently", 900.55); |
| 45 | + employeeManager.addEmployee(employee2); |
| 46 | + employeeManager.updateEmployee(employee2); |
| 47 | + assertThat(employeeManager.getEmployeeCount()).isEqualTo(2); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testRemoveEmployee() throws RuntimeException { |
| 52 | + employee = new Employee(999, "Joseph", "Boxing", 150000000.5); |
| 53 | + employeeManager.addEmployee(employee); |
| 54 | + employeeManager.removeEmployee(999); |
| 55 | + assertThat(employeeManager.getEmployeeCount()).isEqualTo(0); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testRemoveEmployee_errorFromNullEmployeeId() throws RuntimeException { |
| 60 | + assertThatThrownBy(() -> employeeManager.removeEmployee(9)) |
| 61 | + .isInstanceOf(IllegalArgumentException.class) |
| 62 | + .hasMessage("Employee does not in collection with id 9"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testGetEmployeeCount_whenOneEmployeeIsInTheMap() { |
| 67 | + employee = new Employee(999, "Joseph", "Boxing", 150000000.5); |
| 68 | + employeeManager.addEmployee(employee); |
| 69 | + assertThat(employeeManager.getEmployeeCount()).isEqualTo(1); |
| 70 | + } |
| 71 | +} |
0 commit comments