|
1 | 1 | package com.baeldung.assertj; |
2 | 2 |
|
| 3 | +import com.baeldung.assertj.domain.Dog; |
3 | 4 | import org.junit.Test; |
4 | 5 |
|
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.NoSuchElementException; |
| 9 | + |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | +import static org.assertj.core.api.Assertions.withPrecision; |
| 12 | + |
5 | 13 | public class AssertJDemoTest { |
6 | 14 |
|
7 | 15 | @Test |
8 | | - public void shouldTest() throws Exception { |
| 16 | + public void whenComparingReferences_thenNotEqual() throws Exception { |
| 17 | + Dog fido = new Dog("Fido", 5.15f); |
| 18 | + Dog fidosClone = new Dog("Fido", 5.15f); |
| 19 | + |
| 20 | + assertThat(fido).isNotEqualTo(fidosClone); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void whenComparingFields_thenEqual() throws Exception { |
| 25 | + Dog fido = new Dog("Fido", 5.15f); |
| 26 | + Dog fidosClone = new Dog("Fido", 5.15f); |
| 27 | + |
| 28 | + assertThat(fido).isEqualToComparingFieldByFieldRecursively(fidosClone); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void whenCheckingForElement_thenContains() throws Exception { |
| 33 | + List<String> list = Arrays.asList("1", "2", "3"); |
| 34 | + |
| 35 | + assertThat(list) |
| 36 | + .contains("1"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenCheckingForElement_thenMultipleAssertions() throws Exception { |
| 41 | + List<String> list = Arrays.asList("1", "2", "3"); |
| 42 | + |
| 43 | + assertThat(list) |
| 44 | + .isNotEmpty() |
| 45 | + .contains("1") |
| 46 | + .startsWith("1") |
| 47 | + .doesNotContainNull() |
| 48 | + .containsSequence("2", "3"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenCheckingRunnable_thenIsInterface() throws Exception { |
| 53 | + assertThat(Runnable.class).isInterface(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void whenAssigningNSEExToException_thenIsAssignable() throws Exception { |
| 58 | + assertThat(Exception.class).isAssignableFrom(NoSuchElementException.class); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenComparingWithOffset_thenEquals() throws Exception { |
| 63 | + assertThat(5.1).isEqualTo(5, withPrecision(1d)); |
9 | 64 | } |
10 | 65 | } |
0 commit comments