Skip to content

feat: Created unit tests for employee and manager classes - Lesson 15 #490

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 1 commit 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 + "\nName: " + name + "\nDepartment: " + department + "\nSalary: " + salary;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.codedifferently.lesson15;

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

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class EmployeeManagerTest {
EmployeeManager employeeManager;
private Map<Integer, Employee> employees;

@BeforeEach
void setUp() {
employees = new HashMap<>();
employees.put(1, new Employee(1, "John", "Marketing", 20000.50));
employees.put(2, new Employee(2, "Jane", "Accounting", 30000.70));
employeeManager = new EmployeeManager();
for (Map.Entry<Integer, Employee> entry : employees.entrySet()) {
employeeManager.addEmployee(entry.getValue());
}
}

@Test
public void testGetEmployee() {
// Arange
Copy link
Contributor

Choose a reason for hiding this comment

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

Arrange

var id = 1;

// Act
Employee result = employeeManager.getEmployee(id);

// Assert
assertThat(result).isNotNull();
assertThat(result.getName()).isEqualTo("John");
}

@Test
public void testUpdateEmployee() {
// Arrange
var id = 2;

// Act
Employee employee = employeeManager.getEmployee(id); // use this for set test
employee.setDepartment("Human Resources");
employeeManager.updateEmployee(employee);
Employee updatedEmployee = employeeManager.getEmployee(id);

// Assert
assertThat(updatedEmployee).isNotNull();
}

@Test
public void testGetEmployeeCount() {
// Assert
assertThat(employeeManager.getEmployeeCount()).isNotNull();
Copy link
Contributor

Choose a reason for hiding this comment

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

This test can be more robust. You should check the count, add or remove an employee, then check the new count.

}

@Test
public void testRemoveEmployee() {
var id = 1;

int count = employeeManager.getEmployeeCount();
employeeManager.removeEmployee(id);

assertThatThrownBy(() -> employeeManager.getEmployee(id))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Employee does not in collection with id " + id);
assertThat(employeeManager.getEmployeeCount()).isLessThan(count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.codedifferently.lesson15;

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

import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class EmployeeTest {
EmployeeManager employeeManager;
private Map<Integer, Employee> employees;

@BeforeEach
void setUp() {
employees = new HashMap<>();
employees.put(1, new Employee(1, "John", "Marketing", 20000.50));
employees.put(2, new Employee(2, "Jane", "Accounting", 30000.70));
employeeManager = new EmployeeManager();
for (Map.Entry<Integer, Employee> entry : employees.entrySet()) {
employeeManager.addEmployee(entry.getValue());
}
}

@Test
public void testGetSalary() {
// Arrange
var id = 1;

// Act
Employee employee = employeeManager.getEmployee(id);

// Assert
assertThat(employee.getSalary()).isNotNull();
}

@Test
public void testGetDepartment() {
// Arrange
var id = 1;

// Act
Employee employee = employeeManager.getEmployee(id);

// Assert
assertThat(employee.getDepartment()).isNotNull();
}

@Test
public void testGetDetails() {
// Arrange
var id = 1;

// Act
Employee employee = employeeManager.getEmployee(id);

// Assert
assertThat(employee.getDetails()).isNotNull();
assertThat(employee.getName()).isEqualTo("John");
assertThat(employee.getDepartment()).isEqualTo("Marketing");
assertThat(employee.getSalary()).isEqualTo(20000.50);
}
}
Loading