Files
oops/src/test/java/oving5/named/NamedComparatorTest.java
Andreas Omholt Olsen 55c36a603a Add oving 5
2026-02-09 15:28:09 +01:00

51 lines
1.6 KiB
Java

package oving5.named;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class NamedComparatorTest {
private NamedComparator comparator;
private Person1 p1;
private Person2 p2;
@BeforeEach
public void setUp() {
comparator = new NamedComparator();
p1 = new Person1("Aleksander", "Vestlund");
p2 = new Person2("Dan Vestlund");
}
@Test
@DisplayName("Check that people with the same name are equivalent")
public void testSameFullName() {
assertEquals(0, comparator.compare(p1, p1));
assertEquals(0, comparator.compare(p2, p2));
}
@Test
@DisplayName("Check that given names are compared when the family names are the same")
public void testSameFamilyName() {
// Return negative since first givenName is before second
assertTrue(comparator.compare(p1, p2) < 0, "Aleksander should come before Dan");
// Return positive since first givenName is after second
assertTrue(comparator.compare(p2, p1) > 0, "Dan should come after Aleksander");
}
@Test
@DisplayName("Check that family names are compared correctly")
public void testDifferentFamilyName() {
p2.setFamilyName("Bertelsen");
// Return negative since first familyName is before second
assertTrue(comparator.compare(p2, p1) < 0, "Bertelsen should come before Vestlund");
// Return positive since first familyName is after second
assertTrue(comparator.compare(p1, p2) > 0, "Vestlund should come after Bertelsen");
}
}