Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ public void setId(int id) {
this.id = id;
}

// Method to get all details of an employee
public String getDetails() {
return "The Employee's ID: "
+ id
+ " Name: "
+ name
+ " Department: "
+ department
+ " Salary: "
+ salary;
}

// Method to set all employee details at once
public void setDetails(String name, String department, double salary, int id) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.codedifferently.lesson15;

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

import org.junit.jupiter.api.Test;

public class EmployeeManagerTest {

@Test
public void testAddEmployee() {
EmployeeManager employeeManager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employeeManager.addEmployee(employee);
assertThat(employeeManager.getEmployeeCount()).isEqualTo(1);
}

@Test
public void testGetEmployee() {
EmployeeManager employeeManager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employeeManager.addEmployee(employee);
Employee result = employeeManager.getEmployee(1);
assertThat(result).isEqualTo(employee);
}

@Test
public void testUpdateEmployee() {
EmployeeManager employeeManager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employeeManager.addEmployee(employee);
employee.setName("Jane Doe");
employeeManager.updateEmployee(employee);
Employee result = employeeManager.getEmployee(1);
assertThat(result.getName()).isEqualTo("Jane Doe");
}

@Test
public void testRemoveEmployee() {
EmployeeManager employeeManager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employeeManager.addEmployee(employee);
employeeManager.removeEmployee(1);
assertThat(employeeManager.getEmployeeCount()).isEqualTo(0);
}

@Test
public void testEmployeeCount() {
EmployeeManager employeeManager = new EmployeeManager();
Employee employee1 = new Employee(1, "John Doe", "Engineering", 50000);
Employee employee2 = new Employee(2, "Jane Doe", "Marketing", 60000);
employeeManager.addEmployee(employee1);
employeeManager.addEmployee(employee2);
int result = employeeManager.getEmployeeCount();
assertThat(result).isEqualTo(2);
}

@Test
public void assertEmployeeInCollection() {
EmployeeManager employeeManager = new EmployeeManager();
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employeeManager.addEmployee(employee);
try {
employeeManager.getEmployee(1);
} catch (IllegalArgumentException e) {
// This should not happen
assertThat(e.getMessage()).isEqualTo("Employee does not exist in collection with id 1");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.codedifferently.lesson15.Employee;
import org.junit.jupiter.api.Test;

public class EmployeeTest {

@Test
public void testGetDetails() {
// Create an instance of Employee by adding in temporary values
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
// This will call the getDetails method and store that in a variable called result
String result = employee.getDetails();
// This will create a string that we expect the getDetails method to return
String expected = "The Employee's ID: 1 Name: John Doe Department: Engineering Salary: 50000.0";
// This will compare the result of the getDetails method to the expected string
assertThat(result).isEqualTo(expected);
}

@Test
public void testSetDetails() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setDetails("Jane Doe", "Marketing", 60000, 1);

String result = employee.getDetails();
String expected = "The Employee's ID: 1 Name: Jane Doe Department: Marketing Salary: 60000.0";
assertThat(result).isEqualTo(expected);
}

@Test
public void testGetId() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
int result = employee.getId();
assertThat(result).isEqualTo(1);
}

@Test
public void testSetId() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setId(2);
int result = employee.getId();
assertThat(result).isEqualTo(2);
}

@Test
public void testGetName() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
String result = employee.getName();
assertThat(result).isEqualTo("John Doe");
}

@Test
public void testSetName() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setName("Jane Doe");
String result = employee.getName();
assertThat(result).isEqualTo("Jane Doe");
}

@Test
public void testGetDepartment() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
String result = employee.getDepartment();
assertThat(result).isEqualTo("Engineering");
}

@Test
public void testSetDepartment() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setDepartment("Marketing");
String result = employee.getDepartment();
assertThat(result).isEqualTo("Marketing");
}

@Test
public void testGetSalary() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
double result = employee.getSalary();
assertThat(result).isEqualTo(50000);
}

@Test
public void testSetSalary() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setSalary(60000);
double result = employee.getSalary();
assertThat(result).isEqualTo(60000);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.codedifferently.lesson15;

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

import org.junit.jupiter.api.Test;

class Lesson15Test {
Expand All @@ -11,9 +10,81 @@ public void testLesson15() {
assertThat(new Lesson15()).isNotNull();
}

// ------------
// Tests for Employee class
@Test
public void testGetDetails() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Tests for Employee go in their own file. Same for EmployeeManager.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood they are now in their separate classes.

Copy link
Contributor

Choose a reason for hiding this comment

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

You also need to remove these functions from this file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Still not resolved.

// Create an instance of Employee by adding in temporary values
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
// This will call the getDetails method and store that in a variable called result
String result = employee.getDetails();
// This will create a string that we expect the getDetails method to return
String expected = "The Employee's ID: 1 Name: John Doe Department: Engineering Salary: 50000.0";
// This will compare the result of the getDetails method to the expected string
assertThat(result).isEqualTo(expected);
}


@Test
public void testGetId() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
int result = employee.getId();
assertThat(result).isEqualTo(1);
}

@Test
public void testSetId() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setId(2);
int result = employee.getId();
assertThat(result).isEqualTo(2);
}

@Test
public void testGetName() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
String result = employee.getName();
assertThat(result).isEqualTo("John Doe");
}

@Test
public void testGetGreeting() {
// Act
Lesson15.main(null);
public void testSetName() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setName("Jane Doe");
String result = employee.getName();
assertThat(result).isEqualTo("Jane Doe");
}

@Test
public void testGetDepartment() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
String result = employee.getDepartment();
assertThat(result).isEqualTo("Engineering");
}

@Test
public void testSetDepartment() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setDepartment("Marketing");
String result = employee.getDepartment();
assertThat(result).isEqualTo("Marketing");
}

@Test
public void testGetSalary() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
double result = employee.getSalary();
assertThat(result).isEqualTo(50000);
}

@Test
public void testSetSalary() {
Employee employee = new Employee(1, "John Doe", "Engineering", 50000);
employee.setSalary(60000);
double result = employee.getSalary();
assertThat(result).isEqualTo(60000);
}

// -------

}
Loading