diff --git a/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/Employee.java b/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/Employee.java index 49aaabbd..665f0370 100644 --- a/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/Employee.java +++ b/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/Employee.java @@ -45,4 +45,15 @@ public double getSalary() { public void setSalary(double salary) { this.salary = salary; } + + public String getDetails() { + return "Employee ID: " + + this.getId() + + ", Name: " + + this.getName() + + ", Department: " + + this.getDepartment() + + ", Salary: " + + this.getSalary(); + } } diff --git a/lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeManagerTest.java b/lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeManagerTest.java new file mode 100644 index 00000000..eaa3ab98 --- /dev/null +++ b/lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeManagerTest.java @@ -0,0 +1,73 @@ +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.codedifferently.lesson15.Employee; +import com.codedifferently.lesson15.EmployeeManager; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class EmployeeManagerTest { + private EmployeeManager cut; + + @BeforeEach + public void setUp() { + cut = new EmployeeManager(); + Employee employee1 = new Employee(10, "Jojo", "Sales", 22.12); + cut.addEmployee(employee1); + } + + @Test + public void testAddEmployee() { + // Arrange + Employee employee = new Employee(2, "Washington", "Finance", 29.23); + + // Act + cut.addEmployee(employee); + + // Assert + int expectedId = 2; + assertEquals(expectedId, cut.getEmployeeCount()); + } + + @Test + public void testUpdateEmployee() { + // Arrange + Employee employee = new Employee(5, "John", "Safety", 50); + cut.addEmployee(employee); + + // Act + cut.updateEmployee(employee); + + // Assert + int expectedId = 5; + assertEquals(expectedId, employee.getId()); + } + + @Test + public void testRemoveEmployee() { + // Arrange + Employee employee = new Employee(2, "Belvedere", "Finance", 100.1); + cut.addEmployee(employee); + // Act + cut.removeEmployee(employee.getId()); + + // Assert + int removedId = 2; + assertThatThrownBy(() -> cut.removeEmployee(removedId)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Employee does not in collection with id " + removedId); + } + + @Test + public void testGetEmployee() { + // Arrange + Employee employee = new Employee(2, "Washington", "Finance", 29.23); + // Act + cut.addEmployee(employee); + + int employeeId = employee.getId(); + Employee returnedEmployee = cut.getEmployee(employeeId); + // Assert + assertEquals(employee, returnedEmployee); + } +} diff --git a/lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeTest.java b/lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeTest.java new file mode 100644 index 00000000..f869a6c5 --- /dev/null +++ b/lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeTest.java @@ -0,0 +1,79 @@ +package com.codedifferently.lesson15; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class EmployeeTest { + private Employee cut; + + @BeforeEach + public void setUp() { + cut = new Employee(1, "Wilmington", "HR", 200.00); + } + + @Test + public void testGetName() { + // Arrange + cut.setName("Wilmington"); + + // Act + String expectedName = cut.getName(); + + // Assert + String actualName = "Wilmington"; + assertEquals(expectedName, actualName, "The name should be 'Wilmington'"); + } + + @Test + public void testGetId() { + // Arrange + cut.setId(1); + + // Act + int expectedId = cut.getId(); + + // Assert + int actualId = 1; + assertEquals(expectedId, actualId, "The id should be '1'"); + } + + @Test + public void testGetDepartment() { + // Arrange + cut.setDepartment("HR"); + + // Act + String expectedDept = cut.getDepartment(); + + // Assert + String actualDept = "HR"; + assertEquals(expectedDept, actualDept, "The Department should be 'HR'"); + } + + @Test + public void testGetSalary() { + // Arrange + cut.setSalary(200.00); + + // Act + double expectedSalary = cut.getSalary(); + + // Assert + double actualSalary = 200.00; + assertEquals(expectedSalary, actualSalary, "The Salary should be '200.00'"); + } + + @Test + public void testGetDetails() { + // Arrange + String expected = "Employee ID: 1, Name: Wilmington, Department: HR, Salary: 200.0"; + + // Act + String actual = cut.getDetails(); + + // Assert + assertEquals(expected, actual); + } +}