-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for sorting projects by priority then name
Introduced a new unit test class `SortingTest` which includes a setup method for initializing a list of projects. Added a test method to verify sorting projects first by priority and then by name.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
package sorting; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class SortingTest { | ||
|
||
// "Getter" methods on a record match | ||
// the property name, as in name(), priority() | ||
record Project(String name, int priority) {} | ||
|
||
private List<Project> projects; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// create a list of projects | ||
projects = List.of( | ||
new Project("Build a house", 1), | ||
new Project("Build a shed", 2), | ||
new Project("Build a garage", 3), | ||
new Project("Build a barn", 2), | ||
new Project("Build a fence", 1), | ||
new Project("Build a wall", 3) | ||
); | ||
} | ||
|
||
// sort by priority, then by name | ||
@Test | ||
public void sortByPriorityThenName() { | ||
List<Project> sorted = projects.stream() | ||
.sorted(Comparator.comparingInt(Project::priority) | ||
.thenComparing(Project::name)) | ||
.collect(Collectors.toList()); | ||
System.out.println(sorted); | ||
System.out.println(projects); | ||
} | ||
} |