Skip to content

Commit 3268470

Browse files
committed
feat: adds John's Unit Tests for Employee & EmployeeManager classes.
1 parent 50680f2 commit 3268470

File tree

2 files changed

+226
-1
lines changed

2 files changed

+226
-1
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 "The Employee's ID: "
51+
+ id
52+
+ " Name: "
53+
+ name
54+
+ " Department: "
55+
+ department
56+
+ " Salary: "
57+
+ salary;
58+
}
4859
}

lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/Lesson15Test.java

Lines changed: 215 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,221 @@ public void testLesson15() {
1313

1414
@Test
1515
public void testGetGreeting() {
16-
// Act
1716
Lesson15.main(null);
1817
}
18+
19+
@Test
20+
public void getId() {
21+
// Arrange. This initializes an Employee object with an id value, name value, department value,
22+
// and salary value.
23+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
24+
25+
// Act. This retrieves the id value from the Employee object and stores it inside the id
26+
// variable.
27+
int id = employee.getId();
28+
29+
// Assert. This checks if the id value that we retrieved from Employee is equal to 1.
30+
assertThat(id).isEqualTo(5);
31+
}
32+
33+
@Test
34+
public void setId() {
35+
// Arrange.
36+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
37+
38+
// Act.
39+
employee.setId(5);
40+
41+
// Assert.
42+
assertThat(employee.getId()).isEqualTo(5);
43+
}
44+
45+
@Test
46+
public void getName() {
47+
// Arrange.
48+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
49+
50+
// Act.
51+
String name = employee.getName();
52+
53+
// Assert.
54+
assertThat(name).isEqualTo("John Bey");
55+
}
56+
57+
@Test
58+
public void setName() {
59+
// Arrange.
60+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
61+
62+
// Act.
63+
employee.setName("John Bey");
64+
65+
// Assert.
66+
assertThat(employee.getName()).isEqualTo("John Bey");
67+
}
68+
69+
@Test
70+
public void getDepartment() {
71+
// Arrange.
72+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
73+
74+
// Act.
75+
String department = employee.getDepartment();
76+
77+
// Assert.
78+
assertThat(department).isEqualTo("Software Engineering");
79+
}
80+
81+
@Test
82+
public void setDepartment() {
83+
// Arrange.
84+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
85+
86+
// Act.
87+
employee.setDepartment("Software Engineering");
88+
89+
// Assert.
90+
assertThat(employee.getDepartment()).isEqualTo("Software Engineering");
91+
}
92+
93+
@Test
94+
public void getSalary() {
95+
// Arrange.
96+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
97+
98+
// Act.
99+
double salary = employee.getSalary();
100+
101+
// Assert.
102+
assertThat(salary).isEqualTo(200000.0);
103+
}
104+
105+
@Test
106+
public void setSalary() {
107+
// Arrange.
108+
Employee employee = new Employee(5, "John Bey", "Software Engineering", 200000.0);
109+
110+
// Act.
111+
employee.setSalary(200000.0);
112+
113+
// Assert.
114+
assertThat(employee.getSalary()).isEqualTo(200000.0);
115+
}
116+
117+
@Test
118+
public void testGetDetails() {
119+
// Arrange.
120+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
121+
122+
// Act.
123+
String result = employee.getDetails();
124+
125+
// Assert.
126+
String expected =
127+
"The Employee's ID: 1 Name: John Bey Department: Software Engineering Salary: 200000.0";
128+
assertThat(result).isEqualTo(expected);
129+
}
130+
131+
@Test
132+
public void testSetDetails() {
133+
// Arrange.
134+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
135+
136+
// Act.
137+
employee.setId(1);
138+
employee.setName("John Bey");
139+
employee.setDepartment("Software Engineering");
140+
employee.setSalary(200000.0);
141+
142+
// Assert.
143+
String expected =
144+
"The Employee's ID: 1 Name: John Bey Department: Software Engineering Salary: 200000.0";
145+
assertThat(employee.getDetails()).isEqualTo(expected);
146+
}
147+
148+
// Now it's time for us to test the EmployeeManager class. We will be testing the addEmployee,
149+
// getEmployee, updateEmployee, removeEmployee, and getEmployeeCount methods.
150+
151+
@Test
152+
public void testAddEmployee() {
153+
// Arrange.
154+
EmployeeManager employeeManager = new EmployeeManager();
155+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
156+
157+
// Act. Puts the employee object into the employeeManager.
158+
employeeManager.addEmployee(employee);
159+
160+
// Assert.
161+
assertThat(employeeManager.getEmployeeCount()).isEqualTo(1);
162+
}
163+
164+
@Test
165+
public void testGetEmployee() {
166+
// Arrange.
167+
EmployeeManager employeeManager = new EmployeeManager();
168+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
169+
employeeManager.addEmployee(employee);
170+
171+
// Act.
172+
Employee retrievedEmployee = employeeManager.getEmployee(1);
173+
174+
// Assert.
175+
assertThat(retrievedEmployee).isEqualTo(employee);
176+
}
177+
178+
@Test
179+
public void testUpdateEmployee() {
180+
// Arrange.
181+
EmployeeManager employeeManager = new EmployeeManager();
182+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
183+
employeeManager.addEmployee(employee);
184+
Employee updatedEmployee = new Employee(1, "John Doe", "Software Engineering", 250000.0);
185+
186+
// Act.
187+
employeeManager.updateEmployee(updatedEmployee);
188+
189+
// Assert.
190+
assertThat(employeeManager.getEmployee(1).getName()).isEqualTo("John Doe");
191+
}
192+
193+
@Test
194+
public void testRemoveEmployee() {
195+
// Arrange.
196+
EmployeeManager employeeManager = new EmployeeManager();
197+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
198+
employeeManager.addEmployee(employee);
199+
200+
// Act.
201+
employeeManager.removeEmployee(1);
202+
203+
// Assert.
204+
assertThat(employeeManager.getEmployeeCount()).isEqualTo(0);
205+
}
206+
207+
@Test
208+
public void testGetEmployeeCount() {
209+
// Arrange.
210+
EmployeeManager employeeManager = new EmployeeManager();
211+
Employee employee1 = new Employee(1, "John Bey", "Software Engineering", 200000.0);
212+
Employee employee2 = new Employee(2, "Jane Doe", "Marketing", 150000.0);
213+
employeeManager.addEmployee(employee1);
214+
employeeManager.addEmployee(employee2);
215+
216+
// Act.
217+
int count = employeeManager.getEmployeeCount();
218+
219+
// Assert.
220+
assertThat(count).isEqualTo(2);
221+
}
222+
223+
@Test
224+
public void testAssertEmployeeInCollection() {
225+
// Arrange.
226+
EmployeeManager employeeManager = new EmployeeManager();
227+
Employee employee = new Employee(1, "John Bey", "Software Engineering", 200000.0);
228+
employeeManager.addEmployee(employee);
229+
230+
// Act & Assert.
231+
assertThat(employeeManager.getEmployee(1)).isEqualTo(employee);
232+
}
19233
}

0 commit comments

Comments
 (0)