-
Notifications
You must be signed in to change notification settings - Fork 25
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeManagerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
lesson_15/tdd/tdd_app/src/test/java/com/codedifferently/lesson15/EmployeeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arrange