-
Notifications
You must be signed in to change notification settings - Fork 25
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b63f67
feat: created tests with more than 80% code coverage
Cogbonnia e174339
fix: added './gradlew :tdd_app:spotlessApply' script
Cogbonnia fd9ad19
fix: created separate test files
Cogbonnia 20745ba
Merge branch 'code-differently:main' into lesson_15
Cogbonnia cfff55e
Merge branch 'code-differently:main' into lesson_15
Cogbonnia 7864433
Merge branch 'code-differently:main' into lesson_15
Cogbonnia 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
87 changes: 87 additions & 0 deletions
87
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,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); | ||
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"); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
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,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); | ||
} | ||
} |
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.
There should only be one statement in the act phase.