Skip to content

Commit a30c187

Browse files
committed
feat: add more tests to for the input command, Also adds comments to the code for easier reading
1 parent 8df9809 commit a30c187

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void setId(int id) {
2222
this.id = id;
2323
}
2424

25+
// Method to get all details of an employee
2526
public String getDetails() {
2627
return "The Employee's ID: "
2728
+ id
@@ -33,6 +34,7 @@ public String getDetails() {
3334
+ salary;
3435
}
3536

37+
// Method to set all employee details at once
3638
public void setDetails(String name, String department, double salary) {
3739
this.name = name;
3840
this.department = department;

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codedifferently.lesson15;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
45
import org.junit.jupiter.api.Test;
56

67
class Lesson15Test {
@@ -11,19 +12,19 @@ public void testLesson15() {
1112
}
1213

1314
@Test
14-
public void testGetGreeting() {
15-
Lesson15 lesson15 = new Lesson15();
16-
String result = lesson15.getGreeting();
17-
String expected = "Hello, World!";
18-
assertThat(result).isEqualTo(expected);
19-
}
15+
public void testGetGreeting() {}
2016

17+
// ------------
18+
// Tests for Employee class
2119
@Test
2220
public void testGetDetails() {
21+
// Create an instance of Employee by adding in temporary values
2322
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
24-
23+
// This will call the getDetails method and store that in a variable called result
2524
String result = employee.getDetails();
25+
// This will create a string that we expect the getDetails method to return
2626
String expected = "The Employee's ID: 1 Name: John Doe Department: Engineering Salary: 50000.0";
27+
// This will compare the result of the getDetails method to the expected string
2728
assertThat(result).isEqualTo(expected);
2829
}
2930

@@ -52,12 +53,13 @@ public void testSetId() {
5253
assertThat(result).isEqualTo(2);
5354
}
5455

55-
@Test
56+
@Test
5657
public void testGetName() {
5758
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
5859
String result = employee.getName();
5960
assertThat(result).isEqualTo("John Doe");
6061
}
62+
6163
@Test
6264
public void testSetName() {
6365
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
@@ -80,26 +82,32 @@ public void testSetDepartment() {
8082
String result = employee.getDepartment();
8183
assertThat(result).isEqualTo("Marketing");
8284
}
85+
8386
@Test
8487
public void testGetSalary() {
8588
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
8689
double result = employee.getSalary();
8790
assertThat(result).isEqualTo(50000);
8891
}
92+
8993
@Test
9094
public void testSetSalary() {
9195
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
9296
employee.setSalary(60000);
9397
double result = employee.getSalary();
9498
assertThat(result).isEqualTo(60000);
9599
}
100+
101+
// -------
102+
// Tests for EmployeeManager class
96103
@Test
97104
public void testAddEmployee() {
98105
EmployeeManager employeeManager = new EmployeeManager();
99106
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
100107
employeeManager.addEmployee(employee);
101108
assertThat(employeeManager.getEmployeeCount()).isEqualTo(1);
102109
}
110+
103111
@Test
104112
public void testGetEmployee() {
105113
EmployeeManager employeeManager = new EmployeeManager();
@@ -108,6 +116,7 @@ public void testGetEmployee() {
108116
Employee result = employeeManager.getEmployee(1);
109117
assertThat(result).isEqualTo(employee);
110118
}
119+
111120
@Test
112121
public void testUpdateEmployee() {
113122
EmployeeManager employeeManager = new EmployeeManager();
@@ -122,12 +131,33 @@ public void testUpdateEmployee() {
122131
@Test
123132
public void testRemoveEmployee() {
124133
EmployeeManager employeeManager = new EmployeeManager();
125-
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
134+
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
126135
employeeManager.addEmployee(employee);
127136
employeeManager.removeEmployee(1);
128137
assertThat(employeeManager.getEmployeeCount()).isEqualTo(0);
129138
}
130139

131140
@Test
132-
public void test
141+
public void testEmployeeCount() {
142+
EmployeeManager employeeManager = new EmployeeManager();
143+
Employee employee1 = new Employee(1, "John Doe", "Engineering", 50000);
144+
Employee employee2 = new Employee(2, "Jane Doe", "Marketing", 60000);
145+
employeeManager.addEmployee(employee1);
146+
employeeManager.addEmployee(employee2);
147+
int result = employeeManager.getEmployeeCount();
148+
assertThat(result).isEqualTo(2);
149+
}
150+
151+
@Test
152+
public void assertEmployeeInCollection() {
153+
EmployeeManager employeeManager = new EmployeeManager();
154+
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
155+
employeeManager.addEmployee(employee);
156+
try {
157+
employeeManager.getEmployee(1);
158+
} catch (IllegalArgumentException e) {
159+
// This should not happen
160+
assertThat(e.getMessage()).isEqualTo("Employee does not in collection with id 1");
161+
}
162+
}
133163
}

0 commit comments

Comments
 (0)