|
1 | 1 | package com.baeldung.testing.assertj.custom; |
2 | 2 |
|
3 | 3 | import static com.baeldung.testing.assertj.custom.Assertions.assertThat; |
| 4 | +import static org.junit.Assert.fail; |
4 | 5 |
|
5 | 6 | import org.junit.Rule; |
6 | 7 | import org.junit.Test; |
|
9 | 10 | public class AssertJCustomAssertionsUnitTest { |
10 | 11 | @Rule |
11 | 12 | public ExpectedException thrown = ExpectedException.none(); |
12 | | - |
| 13 | + |
13 | 14 | @Test |
14 | | - public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { |
15 | | - thrown.expect(AssertionError.class); |
16 | | - thrown.expectMessage("Expected nickname John but did not have"); |
| 15 | + public void whenPersonNameMatches_thenCorrect() { |
17 | 16 | Person person = new Person("John Doe", 20); |
18 | | - person.addNickname("Nick"); |
19 | | - assertThat(person).hasNickname("John"); |
| 17 | + assertThat(person).hasFullName("John Doe"); |
20 | 18 | } |
21 | 19 |
|
22 | 20 | @Test |
23 | | - public void whenCarIsUsed_thenCorrect() { |
| 21 | + public void whenPersonAgeLessThanEighteen_thenNotAdult() { |
24 | 22 | Person person = new Person("Jane Roe", 16); |
25 | | - Car car = new Car("SUV"); |
26 | | - car.setOwner(person); |
27 | | - assertThat(car).isUsed(); |
| 23 | + |
| 24 | + try { |
| 25 | + assertThat(person).isAdult(); |
| 26 | + fail(); |
| 27 | + } catch (AssertionError e) { |
| 28 | + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected adult but was juvenile"); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void whenPersonDoesNotHaveAMatchingNickname_thenIncorrect() { |
| 34 | + Person person = new Person("John Doe", 20); |
| 35 | + person.addNickname("Nick"); |
| 36 | + |
| 37 | + try { |
| 38 | + assertThat(person).hasNickname("John"); |
| 39 | + fail(); |
| 40 | + } catch (AssertionError e) { |
| 41 | + org.assertj.core.api.Assertions.assertThat(e).hasMessage("Expected nickname John but did not have"); |
| 42 | + } |
28 | 43 | } |
29 | 44 | } |
0 commit comments