109 lines
3.6 KiB
Java
Raw Normal View History

package com.baeldung.assertj;
2016-07-12 14:21:44 +03:00
import org.junit.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import static java.time.LocalDate.ofYearDay;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
public class AssertJJava8UnitTest {
2016-07-12 14:21:44 +03:00
@Test
public void givenOptional_shouldAssert() throws Exception {
final Optional<String> givenOptional = Optional.of("something");
2017-01-29 16:03:33 +02:00
assertThat(givenOptional).isPresent().hasValue("something");
2016-07-12 14:21:44 +03:00
}
@Test
public void givenPredicate_shouldAssert() throws Exception {
final Predicate<String> predicate = s -> s.length() > 4;
2017-01-29 16:03:33 +02:00
assertThat(predicate).accepts("aaaaa", "bbbbb").rejects("a", "b").acceptsAll(asList("aaaaa", "bbbbb")).rejectsAll(asList("a", "b"));
2016-07-12 14:21:44 +03:00
}
@Test
public void givenLocalDate_shouldAssert() throws Exception {
final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8);
final LocalDate todayDate = LocalDate.now();
2017-01-29 16:03:33 +02:00
assertThat(givenLocalDate).isBefore(LocalDate.of(2020, 7, 8)).isAfterOrEqualTo(LocalDate.of(1989, 7, 8));
2016-07-12 14:21:44 +03:00
2017-01-29 16:03:33 +02:00
assertThat(todayDate).isAfter(LocalDate.of(1989, 7, 8)).isToday();
2016-07-12 14:21:44 +03:00
}
@Test
public void givenLocalDateTime_shouldAssert() throws Exception {
final LocalDateTime givenLocalDate = LocalDateTime.of(2016, 7, 8, 12, 0);
2017-01-29 16:03:33 +02:00
assertThat(givenLocalDate).isBefore(LocalDateTime.of(2020, 7, 8, 11, 2));
2016-07-12 14:21:44 +03:00
}
@Test
public void givenLocalTime_shouldAssert() throws Exception {
final LocalTime givenLocalTime = LocalTime.of(12, 15);
2017-01-29 16:03:33 +02:00
assertThat(givenLocalTime).isAfter(LocalTime.of(1, 0)).hasSameHourAs(LocalTime.of(12, 0));
2016-07-12 14:21:44 +03:00
}
@Test
public void givenList_shouldAssertFlatExtracting() throws Exception {
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
2017-01-29 16:03:33 +02:00
assertThat(givenList).flatExtracting(LocalDate::getYear).contains(2015);
2016-07-12 14:21:44 +03:00
}
2016-07-12 21:14:57 +03:00
@Test
public void givenList_shouldAssertFlatExtractingLeapYear() throws Exception {
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
2017-01-29 16:03:33 +02:00
assertThat(givenList).flatExtracting(LocalDate::isLeapYear).contains(true);
2016-07-12 21:14:57 +03:00
}
@Test
public void givenList_shouldAssertFlatExtractingClass() throws Exception {
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
2017-01-29 16:03:33 +02:00
assertThat(givenList).flatExtracting(Object::getClass).contains(LocalDate.class);
2016-07-12 21:14:57 +03:00
}
2016-07-12 14:21:44 +03:00
@Test
public void givenList_shouldAssertMultipleFlatExtracting() throws Exception {
final List<LocalDate> givenList = asList(ofYearDay(2016, 5), ofYearDay(2015, 6));
2017-01-29 16:03:33 +02:00
assertThat(givenList).flatExtracting(LocalDate::getYear, LocalDate::getDayOfMonth).contains(2015, 6);
2016-07-12 14:21:44 +03:00
}
@Test
public void givenString_shouldSatisfy() throws Exception {
final String givenString = "someString";
2017-01-29 16:03:33 +02:00
assertThat(givenString).satisfies(s -> {
assertThat(s).isNotEmpty();
assertThat(s).hasSize(10);
});
2016-07-12 14:21:44 +03:00
}
@Test
public void givenString_shouldMatch() throws Exception {
2016-07-12 21:33:20 +03:00
final String emptyString = "";
2016-07-12 14:21:44 +03:00
2017-01-29 16:03:33 +02:00
assertThat(emptyString).matches(String::isEmpty);
2016-07-12 14:21:44 +03:00
}
@Test
public void givenList_shouldHasOnlyOneElementSatisfying() throws Exception {
final List<String> givenList = Arrays.asList("");
2017-01-29 16:03:33 +02:00
assertThat(givenList).hasOnlyOneElementSatisfying(s -> assertThat(s).isEmpty());
2016-07-12 14:21:44 +03:00
}
}