diff --git a/assertj/pom.xml b/assertj/pom.xml index ce97278a97..26f45cfa26 100644 --- a/assertj/pom.xml +++ b/assertj/pom.xml @@ -9,10 +9,16 @@ 1.0.0-SNAPSHOT + + com.google.guava + guava + 19.0 + junit junit 4.12 + test org.assertj @@ -20,6 +26,11 @@ 3.4.1 test + + org.assertj + assertj-guava + 3.0.0 + diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJGuavaTest.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJGuavaTest.java new file mode 100644 index 0000000000..95faaec3d0 --- /dev/null +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJGuavaTest.java @@ -0,0 +1,91 @@ +package com.baeldung.assertj.introduction; + +import com.google.common.base.Optional; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Multimaps; +import com.google.common.collect.Range; +import com.google.common.collect.SetMultimap; +import com.google.common.collect.TreeRangeMap; +import com.google.common.io.Files; +import org.assertj.guava.data.MapEntry; +import org.junit.Test; + +import java.io.File; + +import static org.assertj.guava.api.Assertions.assertThat; +import static org.assertj.guava.api.Assertions.entry; + +public class AssertJGuavaTest { + + @Test + public void givenTwoEmptyFiles_whenComparingContent_thenEqual() throws Exception { + final File temp = File.createTempFile("bael", "dung"); + final File temp2 = File.createTempFile("bael", "dung2"); + + assertThat(Files.asByteSource(temp)) + .hasSize(0) + .hasSameContentAs(Files.asByteSource(temp2)); + } + + @Test + public void givenMultimap_whenVerifying_thenCorrect() throws Exception { + final SetMultimap mmap = Multimaps.forMap(ImmutableMap.of(1, "one")); + + assertThat(mmap) + .hasSize(1) + .containsKeys(1) + .contains(entry(1, "one")); + } + + @Test + public void givenOptional_whenVerifyingContent_thenShouldBeEqual() throws Exception { + final Optional something = Optional.of("something"); + + assertThat(something) + .isPresent() + .extractingValue() + .isEqualTo("something"); + } + + @Test + public void givenRange_whenVerifying_thenShouldBeCorrect() throws Exception { + final Range range = Range.openClosed("a", "g"); + + assertThat(range) + .hasOpenedLowerBound() + .isNotEmpty() + .hasClosedUpperBound() + .contains("b"); + } + + @Test + public void givenRangeMap_whenVerifying_thenShouldBeCorrect() throws Exception { + final TreeRangeMap map = TreeRangeMap.create(); + + map.put(Range.closed(0, 60), "F"); + map.put(Range.closed(61, 70), "D"); + + assertThat(map) + .isNotEmpty() + .containsKeys(0) + .contains(MapEntry.entry(34, "F")); + } + + @Test + public void givenTable_whenVerifying_thenShouldBeCorrect() throws Exception { + final HashBasedTable table = HashBasedTable.create(2, 2); + + table.put(1, "A", "PRESENT"); + table.put(1, "B", "ABSENT"); + + assertThat(table) + .hasRowCount(1) + .containsValues("ABSENT") + .containsCell(1, "B", "ABSENT"); + } + + + + +}