Skip to content

feat: created tests with more than 80% code coverage #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public double getSalary() {
public void setSalary(double salary) {
this.salary = salary;
}

public String getDetails() {
return "ID: " + id + ", Name: " + name + ", Department: " + department + ", Salary: " + salary;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.codedifferently.lesson15;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;

public class EmployeeManagerTest {
@Test
void testAddEmployee() {
// Arrange
EmployeeManager manager = new EmployeeManager();
Employee employee = new Employee(1, "Jane Doe", "Marketing", 65000.00);

// Act
manager.addEmployee(employee);
Employee retrievedEmployee = manager.getEmployee(1);

// Assert
assertThat(retrievedEmployee).isNotNull();
assertThat(retrievedEmployee.getName()).isEqualTo("Jane Doe");
}

@Test
void testUpdateEmployee() {
// Arrange
EmployeeManager manager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);
manager.addEmployee(employee);

// Act
employee.setName("John Smith");
manager.updateEmployee(employee);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should only be one statement in the act phase.

Employee updatedEmployee = manager.getEmployee(1);

// Assert
assertThat(updatedEmployee.getName()).isEqualTo("John Smith");
}

@Test
void testEmployeeCount() {
// Arrange
EmployeeManager manager = new EmployeeManager();
manager.addEmployee(new Employee(1, "John Doe", "Engineering", 75000.00));
manager.addEmployee(new Employee(2, "Jane Doe", "Marketing", 65000.00));

// Act
int count = manager.getEmployeeCount();

// Assert
assertThat(count).isEqualTo(2);
}

@Test
void testRemoveEmployeeThrowsExceptionWhenNotFound() {
// Arrange
EmployeeManager manager = new EmployeeManager();

// Act & Assert
assertThatThrownBy(() -> manager.removeEmployee(99))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Employee does not in collection with id 99");
}

@Test
void testGetEmployeeThrowsExceptionWhenNotFound() {
// Arrange
EmployeeManager manager = new EmployeeManager();

// Act & Assert
assertThatThrownBy(() -> manager.getEmployee(99))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Employee does not in collection with id 99");
}

@Test
void testUpdateEmployeeThrowsExceptionWhenNotFound() {
// Arrange
EmployeeManager manager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);

// Act & Assert
assertThatThrownBy(() -> manager.updateEmployee(employee))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Employee does not in collection with id 1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.codedifferently.lesson15;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

public class EmployeeTest {
@Test
void testEmployeeCreationAndDetails() {
// Arrange
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);

// Act
String details = employee.getDetails();

// Assert
assertThat(details)
.isEqualTo("ID: 1, Name: John Doe, Department: Engineering, Salary: 75000.0");
}

@Test
void testGetters() {
// Arrange
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);

// Act & Assert
assertThat(employee.getId()).isEqualTo(1);
assertThat(employee.getName()).isEqualTo("John Doe");
assertThat(employee.getDepartment()).isEqualTo("Engineering");
assertThat(employee.getSalary()).isEqualTo(75000.00);
}

@Test
void testSetters() {
// Arrange
Employee employee = new Employee(1, "John Doe", "Engineering", 75000.00);

// Act
employee.setId(2);
employee.setName("Jane Doe");
employee.setDepartment("Marketing");
employee.setSalary(65000.00);

// Assert
assertThat(employee.getId()).isEqualTo(2);
assertThat(employee.getName()).isEqualTo("Jane Doe");
assertThat(employee.getDepartment()).isEqualTo("Marketing");
assertThat(employee.getSalary()).isEqualTo(65000.00);
}
}
Loading