Skip to content

Instantly share code, notes, and snippets.

@joshden joshden/Person.java
Last active Jan 15, 2017

Embed
What would you like to do?
Example demonstrating JUnit 5 and Java 8 date & Supplier with an immutable, testable class
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.function.Supplier;
class Person {
private final String givenName;
private final String surname;
private final LocalDate dateOfBirth;
private final Supplier<LocalDate> currentDateSupplier;
Person(String givenName, String surname, LocalDate dateOfBirth) {
this(givenName, surname, dateOfBirth, LocalDate::now);
}
Person(String givenName, String surname, LocalDate dateOfBirth, Supplier<LocalDate> currentDateSupplier) {
this.givenName = givenName;
this.surname = surname;
this.dateOfBirth = dateOfBirth;
this.currentDateSupplier = currentDateSupplier;
}
String getDisplayName() {
return surname + ", " + givenName;
}
long getAge() {
return ChronoUnit.YEARS.between(dateOfBirth, currentDateSupplier.get());
}
long getDaysUntilBirthday() {
LocalDate now = currentDateSupplier.get();
LocalDate nextBirthday = getNextBirthday(now);
return ChronoUnit.DAYS.between(now, nextBirthday);
}
private LocalDate getNextBirthday(LocalDate now) {
LocalDate thisYearBirthday = dateOfBirth.withYear(now.getYear());
if (now.isAfter(thisYearBirthday)) {
return thisYearBirthday.plusYears(1);
}
else {
return thisYearBirthday;
}
}
public static void main(String... args) {
Person person = new Person("Josh", "Hayden", LocalDate.of(2009, 1, 12));
System.out.println(person.getDisplayName() + ": " + person.getAge() + " years old");
System.out.println("Next birthday in " + person.getDaysUntilBirthday() + " days");
}
}
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.time.LocalDate;
import java.util.function.Function;
import java.util.function.Supplier;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
class PersonTest {
private final Supplier<LocalDate> currentDateSupplier = ()-> LocalDate.parse("2015-05-02");
private final LocalDate ageJustOver5 = LocalDate.parse("2010-05-01");
private final LocalDate ageExactly5 = LocalDate.parse("2010-05-02");
private final LocalDate ageAlmost5 = LocalDate.parse("2010-05-03");
@Test
void testGetDisplayName() {
Person person = new Person("Josh", "Hayden", ageExactly5, currentDateSupplier);
String displayName = person.getDisplayName();
assertThat(displayName, equalTo("Hayden, Josh"));
}
@Test
void testGetAge() {
assertAll(
createPersonAndAssertValue(ageAlmost5, 4, Person::getAge),
createPersonAndAssertValue(ageExactly5, 5, Person::getAge),
createPersonAndAssertValue(ageJustOver5, 5, Person::getAge)
);
}
@Test
void testGetDaysUntilBirthday() {
assertAll(
createPersonAndAssertValue(ageAlmost5, 1, Person::getDaysUntilBirthday),
createPersonAndAssertValue(ageExactly5, 0, Person::getDaysUntilBirthday),
createPersonAndAssertValue(ageJustOver5, 365, Person::getDaysUntilBirthday)
);
}
private Executable createPersonAndAssertValue(LocalDate dateOfBirth, long expectedValue, Function<Person, Long> personLongFunction) {
Person person = new Person("Given", "Sur", dateOfBirth, currentDateSupplier);
long actualValue = personLongFunction.apply(person);
return () -> assertEquals(expectedValue, actualValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.