Skip to content

Commit

Permalink
Add unit test for sorting projects by priority then name
Browse files Browse the repository at this point in the history
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
kousen committed Sep 18, 2024
1 parent cc33362 commit ee78cf7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/test/java/sorting/SortingTest.java
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);
}
}

0 comments on commit ee78cf7

Please sign in to comment.