|
| 1 | +package com.baeldung.assertj.introduction; |
| 2 | + |
| 3 | +import com.baeldung.assertj.introduction.domain.Dog; |
| 4 | +import com.baeldung.assertj.introduction.domain.Person; |
| 5 | +import org.assertj.core.util.Maps; |
| 6 | +import org.junit.Ignore; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.File; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.NoSuchElementException; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static org.assertj.core.api.Assertions.entry; |
| 19 | +import static org.assertj.core.api.Assertions.withPrecision; |
| 20 | + |
| 21 | +public class AssertJCoreTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + public void whenComparingReferences_thenNotEqual() throws Exception { |
| 25 | + Dog fido = new Dog("Fido", 5.15f); |
| 26 | + Dog fidosClone = new Dog("Fido", 5.15f); |
| 27 | + |
| 28 | + assertThat(fido).isNotEqualTo(fidosClone); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void whenComparingFields_thenEqual() throws Exception { |
| 33 | + Dog fido = new Dog("Fido", 5.15f); |
| 34 | + Dog fidosClone = new Dog("Fido", 5.15f); |
| 35 | + |
| 36 | + assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenCheckingForElement_thenContains() throws Exception { |
| 41 | + List<String> list = Arrays.asList("1", "2", "3"); |
| 42 | + |
| 43 | + assertThat(list) |
| 44 | + .contains("1"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenCheckingForElement_thenMultipleAssertions() throws Exception { |
| 49 | + List<String> list = Arrays.asList("1", "2", "3"); |
| 50 | + |
| 51 | + assertThat(list).isNotEmpty(); |
| 52 | + assertThat(list).startsWith("1"); |
| 53 | + assertThat(list).doesNotContainNull(); |
| 54 | + |
| 55 | + assertThat(list) |
| 56 | + .isNotEmpty() |
| 57 | + .contains("1") |
| 58 | + .startsWith("1") |
| 59 | + .doesNotContainNull() |
| 60 | + .containsSequence("2", "3"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void whenCheckingRunnable_thenIsInterface() throws Exception { |
| 65 | + assertThat(Runnable.class).isInterface(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void whenCheckingCharacter_thenIsUnicode() throws Exception { |
| 70 | + char someCharacter = 'c'; |
| 71 | + |
| 72 | + assertThat(someCharacter) |
| 73 | + .isNotEqualTo('a') |
| 74 | + .inUnicode() |
| 75 | + .isGreaterThanOrEqualTo('b') |
| 76 | + .isLowerCase(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { |
| 81 | + assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void whenComparingWithOffset_thenEquals() throws Exception { |
| 86 | + assertThat(5.1).isEqualTo(5, withPrecision(1d)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void whenCheckingString_then() throws Exception { |
| 91 | + assertThat("".isEmpty()).isTrue(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void whenCheckingFile_then() throws Exception { |
| 96 | + final File someFile = File.createTempFile("aaa", "bbb"); |
| 97 | + someFile.deleteOnExit(); |
| 98 | + |
| 99 | + assertThat(someFile) |
| 100 | + .exists() |
| 101 | + .isFile() |
| 102 | + .canRead() |
| 103 | + .canWrite(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void whenCheckingIS_then() throws Exception { |
| 108 | + InputStream given = new ByteArrayInputStream("foo".getBytes()); |
| 109 | + InputStream expected = new ByteArrayInputStream("foo".getBytes()); |
| 110 | + |
| 111 | + assertThat(given).hasSameContentAs(expected); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void whenGivenMap_then() throws Exception { |
| 116 | + Map<Integer, String> map = Maps.newHashMap(2, "a"); |
| 117 | + |
| 118 | + assertThat(map) |
| 119 | + .isNotEmpty() |
| 120 | + .containsKey(2) |
| 121 | + .doesNotContainKeys(10) |
| 122 | + .contains(entry(2, "a")); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void whenGivenException_then() throws Exception { |
| 127 | + Exception ex = new Exception("abc"); |
| 128 | + |
| 129 | + assertThat(ex) |
| 130 | + .hasNoCause() |
| 131 | + .hasMessageEndingWith("c"); |
| 132 | + } |
| 133 | + |
| 134 | + @Ignore // IN ORDER TO TEST, REMOVE THIS LINE |
| 135 | + @Test |
| 136 | + public void whenRunningAssertion_thenDescribed() throws Exception { |
| 137 | + Person person = new Person("Alex", 34); |
| 138 | + |
| 139 | + assertThat(person.getAge()) |
| 140 | + .as("%s's age should be equal to 100") |
| 141 | + .isEqualTo(100); |
| 142 | + } |
| 143 | +} |
0 commit comments