From 024b4f8f614debf5c20da774bd438534d03cdd4f Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 23 Jun 2016 17:33:59 +0200 Subject: [PATCH] Add additional AssertJ examples --- .gitignore | 1 + ...ertJDemoTest.java => AssertJCoreTest.java} | 68 ++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) rename assertj/src/test/java/com/baeldung/assertj/introduction/{AssertJDemoTest.java => AssertJCoreTest.java} (54%) diff --git a/.gitignore b/.gitignore index e841cc4bf5..319eb5ca2d 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ target/ spring-openid/src/main/resources/application.properties .recommenders/ + diff --git a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJDemoTest.java b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java similarity index 54% rename from assertj/src/test/java/com/baeldung/assertj/introduction/AssertJDemoTest.java rename to assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java index 729c252c3c..21bc40ae9f 100644 --- a/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJDemoTest.java +++ b/assertj/src/test/java/com/baeldung/assertj/introduction/AssertJCoreTest.java @@ -2,17 +2,23 @@ package com.baeldung.assertj.introduction; import com.baeldung.assertj.introduction.domain.Dog; import com.baeldung.assertj.introduction.domain.Person; +import org.assertj.core.util.Maps; import org.junit.Ignore; import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.InputStream; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.NoSuchElementException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.withPrecision; -public class AssertJDemoTest { +public class AssertJCoreTest { @Test public void whenComparingReferences_thenNotEqual() throws Exception { @@ -42,6 +48,10 @@ public class AssertJDemoTest { public void whenCheckingForElement_thenMultipleAssertions() throws Exception { List list = Arrays.asList("1", "2", "3"); + assertThat(list).isNotEmpty(); + assertThat(list).startsWith("1"); + assertThat(list).doesNotContainNull(); + assertThat(list) .isNotEmpty() .contains("1") @@ -55,6 +65,17 @@ public class AssertJDemoTest { assertThat(Runnable.class).isInterface(); } + @Test + public void whenCheckingCharacter_thenIsUnicode() throws Exception { + char someCharacter = 'c'; + + assertThat(someCharacter) + .isNotEqualTo('a') + .inUnicode() + .isGreaterThanOrEqualTo('b') + .isLowerCase(); + } + @Test public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); @@ -65,6 +86,51 @@ public class AssertJDemoTest { assertThat(5.1).isEqualTo(5, withPrecision(1d)); } + @Test + public void whenCheckingString_then() throws Exception { + assertThat("".isEmpty()).isTrue(); + } + + @Test + public void whenCheckingFile_then() throws Exception { + final File someFile = File.createTempFile("aaa", "bbb"); + someFile.deleteOnExit(); + + assertThat(someFile) + .exists() + .isFile() + .canRead() + .canWrite(); + } + + @Test + public void whenCheckingIS_then() throws Exception { + InputStream given = new ByteArrayInputStream("foo".getBytes()); + InputStream expected = new ByteArrayInputStream("foo".getBytes()); + + assertThat(given).hasSameContentAs(expected); + } + + @Test + public void whenGivenMap_then() throws Exception { + Map map = Maps.newHashMap(2, "a"); + + assertThat(map) + .isNotEmpty() + .containsKey(2) + .doesNotContainKeys(10) + .contains(entry(2, "a")); + } + + @Test + public void whenGivenException_then() throws Exception { + Exception ex = new Exception("abc"); + + assertThat(ex) + .hasNoCause() + .hasMessageEndingWith("c"); + } + @Ignore // IN ORDER TO TEST, REMOVE THIS LINE @Test public void whenRunningAssertion_thenDescribed() throws Exception {