Skip to content

Commit 0b63f67

Browse files
committed
feat: created tests with more than 80% code coverage
1 parent 43b0c16 commit 0b63f67

File tree

2 files changed

+134
-9
lines changed

2 files changed

+134
-9
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ public double getSalary() {
4545
public void setSalary(double salary) {
4646
this.salary = salary;
4747
}
48+
public String getDetails() {
49+
return "ID: " + id + ", Name: " + name + ", Department: " + department + ", Salary: " + salary;
50+
}
4851
}
Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,141 @@
11
package com.codedifferently.lesson15;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45

56
import org.junit.jupiter.api.Test;
67

78
class Lesson15Test {
89

9-
@Test
10-
public void testLesson15() {
11-
assertThat(new Lesson15()).isNotNull();
12-
}
10+
@Test
11+
public void testLesson15() {
12+
assertThat(new Lesson15()).isNotNull();
13+
}
1314

14-
@Test
15-
public void testGetGreeting() {
16-
// Act
17-
Lesson15.main(null);
18-
}
15+
@Test
16+
public void testGetGreeting() {
17+
// Act
18+
Lesson15.main(null);
19+
}
20+
21+
@Test
22+
void testAddEmployee() {
23+
// Arrange
24+
EmployeeManager manager = new EmployeeManager();
25+
Employee employee = new Employee(1, "Jane Doe", "Marketing", 65000.00);
26+
27+
// Act
28+
manager.addEmployee(employee);
29+
Employee retrievedEmployee = manager.getEmployee(1);
30+
31+
// Assert
32+
assertThat(retrievedEmployee).isNotNull();
33+
assertThat(retrievedEmployee.getName()).isEqualTo("Jane Doe");
34+
}
35+
36+
@Test
37+
void testUpdateEmployee() {
38+
// Arrange
39+
EmployeeManager manager = new EmployeeManager();
40+
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);
41+
manager.addEmployee(employee);
42+
43+
// Act
44+
employee.setName("John Smith");
45+
manager.updateEmployee(employee);
46+
Employee updatedEmployee = manager.getEmployee(1);
47+
48+
// Assert
49+
assertThat(updatedEmployee.getName()).isEqualTo("John Smith");
50+
}
51+
52+
@Test
53+
void testEmployeeCount() {
54+
// Arrange
55+
EmployeeManager manager = new EmployeeManager();
56+
manager.addEmployee(new Employee(1, "John Doe", "Engineering", 75000.00));
57+
manager.addEmployee(new Employee(2, "Jane Doe", "Marketing", 65000.00));
58+
59+
// Act
60+
int count = manager.getEmployeeCount();
61+
62+
// Assert
63+
assertThat(count).isEqualTo(2);
64+
}
65+
66+
@Test
67+
void testRemoveEmployeeThrowsExceptionWhenNotFound() {
68+
// Arrange
69+
EmployeeManager manager = new EmployeeManager();
70+
71+
// Act & Assert
72+
assertThatThrownBy(() -> manager.removeEmployee(99))
73+
.isInstanceOf(IllegalArgumentException.class)
74+
.hasMessageContaining("Employee does not in collection with id 99");
75+
}
76+
77+
@Test
78+
void testGetEmployeeThrowsExceptionWhenNotFound() {
79+
// Arrange
80+
EmployeeManager manager = new EmployeeManager();
81+
82+
// Act & Assert
83+
assertThatThrownBy(() -> manager.getEmployee(99))
84+
.isInstanceOf(IllegalArgumentException.class)
85+
.hasMessageContaining("Employee does not in collection with id 99");
86+
}
87+
88+
@Test
89+
void testUpdateEmployeeThrowsExceptionWhenNotFound() {
90+
// Arrange
91+
EmployeeManager manager = new EmployeeManager();
92+
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);
93+
94+
// Act & Assert
95+
assertThatThrownBy(() -> manager.updateEmployee(employee))
96+
.isInstanceOf(IllegalArgumentException.class)
97+
.hasMessageContaining("Employee does not in collection with id 1");
98+
}
99+
100+
@Test
101+
void testEmployeeCreationAndDetails() {
102+
// Arrange
103+
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);
104+
105+
// Act
106+
String details = employee.getDetails();
107+
108+
// Assert
109+
assertThat(details).isEqualTo("ID: 1, Name: John Doe, Department: Engineering, Salary: 75000.0");
110+
}
111+
112+
@Test
113+
void testGetters() {
114+
// Arrange
115+
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);
116+
117+
// Act & Assert
118+
assertThat(employee.getId()).isEqualTo(1);
119+
assertThat(employee.getName()).isEqualTo("John Doe");
120+
assertThat(employee.getDepartment()).isEqualTo("Engineering");
121+
assertThat(employee.getSalary()).isEqualTo(75000.00);
122+
}
123+
124+
@Test
125+
void testSetters() {
126+
// Arrange
127+
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);
128+
129+
// Act
130+
employee.setId(2);
131+
employee.setName("Jane Doe");
132+
employee.setDepartment("Marketing");
133+
employee.setSalary(65000.00);
134+
135+
// Assert
136+
assertThat(employee.getId()).isEqualTo(2);
137+
assertThat(employee.getName()).isEqualTo("Jane Doe");
138+
assertThat(employee.getDepartment()).isEqualTo("Marketing");
139+
assertThat(employee.getSalary()).isEqualTo(65000.00);
140+
}
19141
}

0 commit comments

Comments
 (0)