22
33import com .baeldung .assertj .introduction .domain .Dog ;
44import com .baeldung .assertj .introduction .domain .Person ;
5+ import org .assertj .core .util .Maps ;
56import org .junit .Ignore ;
67import org .junit .Test ;
78
9+ import java .io .ByteArrayInputStream ;
10+ import java .io .File ;
11+ import java .io .InputStream ;
812import java .util .Arrays ;
913import java .util .List ;
14+ import java .util .Map ;
1015import java .util .NoSuchElementException ;
1116
1217import static org .assertj .core .api .Assertions .assertThat ;
18+ import static org .assertj .core .api .Assertions .entry ;
1319import static org .assertj .core .api .Assertions .withPrecision ;
1420
15- public class AssertJDemoTest {
21+ public class AssertJCoreTest {
1622
1723 @ Test
1824 public void whenComparingReferences_thenNotEqual () throws Exception {
@@ -42,6 +48,10 @@ public void whenCheckingForElement_thenContains() throws Exception {
4248 public void whenCheckingForElement_thenMultipleAssertions () throws Exception {
4349 List <String > list = Arrays .asList ("1" , "2" , "3" );
4450
51+ assertThat (list ).isNotEmpty ();
52+ assertThat (list ).startsWith ("1" );
53+ assertThat (list ).doesNotContainNull ();
54+
4555 assertThat (list )
4656 .isNotEmpty ()
4757 .contains ("1" )
@@ -55,6 +65,17 @@ public void whenCheckingRunnable_thenIsInterface() throws Exception {
5565 assertThat (Runnable .class ).isInterface ();
5666 }
5767
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+
5879 @ Test
5980 public void whenAssigningNSEExToException_thenIsAssignable () throws Exception {
6081 assertThat (Exception .class ).isAssignableFrom (NoSuchElementException .class );
@@ -65,6 +86,51 @@ public void whenComparingWithOffset_thenEquals() throws Exception {
6586 assertThat (5.1 ).isEqualTo (5 , withPrecision (1d ));
6687 }
6788
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+
68134 @ Ignore // IN ORDER TO TEST, REMOVE THIS LINE
69135 @ Test
70136 public void whenRunningAssertion_thenDescribed () throws Exception {
0 commit comments