Skip to content

Commit c00f98a

Browse files
committed
feat: added lesson15 unit test homework by Yemi
1 parent 112c87a commit c00f98a

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/Employee.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,15 @@ public double getSalary() {
4545
public void setSalary(double salary) {
4646
this.salary = salary;
4747
}
48+
49+
public String getDetails() {
50+
return "Employee ID: "
51+
+ this.getId()
52+
+ ", Name: "
53+
+ this.getName()
54+
+ ", Department: "
55+
+ this.getDepartment()
56+
+ ", Salary: "
57+
+ this.getSalary();
58+
}
4859
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import static org.junit.jupiter.api.Assertions.assertNull;
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+
private EmployeeManager cut;
11+
12+
@BeforeEach
13+
public void setUp() {
14+
cut = new EmployeeManager();
15+
Employee employee1 = new Employee(10, "Jojo", "Sales", 22.12);
16+
cut.addEmployee(employee1);
17+
}
18+
19+
@Test
20+
public void testAddEmployee() {
21+
// Arrange
22+
Employee employee = new Employee(2, "Washington", "Finance", 29.23);
23+
24+
// Act
25+
cut.addEmployee(employee);
26+
27+
// Assert
28+
int expectedId = 2;
29+
assertEquals(expectedId, cut.getEmployeeCount());
30+
}
31+
32+
@Test
33+
public void testgetEmployee() {
34+
// Arrange
35+
Employee employee = new Employee(2, "Washington", "Finance", 29.23);
36+
37+
// Act
38+
Employee returnedEmployee = cut.getEmployee(employee.getId());
39+
40+
// Assert
41+
assertNull(returnedEmployee);
42+
}
43+
44+
@Test
45+
public void testRemoveEmployee() {
46+
// Arrange
47+
Employee employee = new Employee(3, "Belvedere", "Finance", 100.1);
48+
49+
// Act
50+
cut.removeEmployee(employee.getId());
51+
52+
// Assert
53+
int removedId = 3;
54+
assertNull(cut.getEmployee(removedId));
55+
}
56+
57+
@Test
58+
public void testUpdateEmployee() {
59+
// Arrange
60+
Employee employee = new Employee(5, "John", "Safety", 50);
61+
62+
// Act
63+
cut.updateEmployee(employee);
64+
65+
// Assert
66+
int expectedId = 5;
67+
assertEquals(expectedId, employee.getId());
68+
}
69+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.codedifferently.lesson15;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class EmployeeTest {
9+
private Employee cut;
10+
11+
@BeforeEach
12+
public void setUp() {
13+
cut = new Employee(1, "Wilmington", "HR", 200.00);
14+
}
15+
16+
@Test
17+
public void testGetName() {
18+
// Arrange
19+
cut.setName("Wilmington");
20+
21+
// Act
22+
String expectedName = cut.getName();
23+
24+
// Assert
25+
String actualName = "Wilmington";
26+
assertEquals(expectedName, actualName, "The name should be 'Wilmington'");
27+
}
28+
29+
@Test
30+
public void testGetId() {
31+
// Arrange
32+
cut.setId(1);
33+
34+
// Act
35+
int expectedId = cut.getId();
36+
37+
// Assert
38+
int actualId = 1;
39+
assertEquals(expectedId, actualId, "The id should be '1'");
40+
}
41+
42+
@Test
43+
public void testGetDepartment() {
44+
// Arrange
45+
cut.setDepartment("HR");
46+
47+
// Act
48+
String expectedDept = cut.getDepartment();
49+
50+
// Assert
51+
String actualDept = "HR";
52+
assertEquals(expectedDept, actualDept, "The Department should be 'HR'");
53+
}
54+
55+
@Test
56+
public void testGetSalary() {
57+
// Arrange
58+
cut.setSalary(200.00);
59+
60+
// Act
61+
double expectedSalary = cut.getSalary();
62+
63+
// Assert
64+
double actualSalary = cut.getSalary();
65+
assertEquals(expectedSalary, actualSalary, "The Salary should be '200.00'");
66+
}
67+
68+
@Test
69+
public void testGetDetails() {
70+
// Arrange
71+
String expected = "Employee ID: 1, Name: Wilmington, Department: HR, Salary: 200.0";
72+
73+
// Act
74+
String actual = cut.getDetails();
75+
76+
// Assert
77+
assertEquals(expected, actual);
78+
}
79+
}

0 commit comments

Comments
 (0)