1+ package in .ravidsrk .sample ;
2+
3+ import org .hamcrest .core .CombinableMatcher ;
4+ import org .junit .Test ;
5+
6+ import java .util .Arrays ;
7+
8+ import static org .hamcrest .CoreMatchers .allOf ;
9+ import static org .hamcrest .CoreMatchers .anyOf ;
10+ import static org .hamcrest .CoreMatchers .both ;
11+ import static org .hamcrest .CoreMatchers .containsString ;
12+ import static org .hamcrest .CoreMatchers .equalTo ;
13+ import static org .hamcrest .CoreMatchers .everyItem ;
14+ import static org .hamcrest .CoreMatchers .hasItems ;
15+ import static org .hamcrest .CoreMatchers .not ;
16+ import static org .hamcrest .CoreMatchers .sameInstance ;
17+ import static org .hamcrest .CoreMatchers .startsWith ;
18+ import static org .junit .Assert .assertArrayEquals ;
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .junit .Assert .assertFalse ;
21+ import static org .junit .Assert .assertNotNull ;
22+ import static org .junit .Assert .assertNotSame ;
23+ import static org .junit .Assert .assertNull ;
24+ import static org .junit .Assert .assertSame ;
25+ import static org .junit .Assert .assertThat ;
26+ import static org .junit .Assert .assertTrue ;
27+
28+ /**
29+ * Created by Ravindra Kumar on 16/09/16.
30+ */
31+
32+ public class AssertTests {
33+ @ Test
34+ public void testAssertArrayEquals () {
35+ byte [] expected = "trial" .getBytes ();
36+ byte [] actual = "trial" .getBytes ();
37+ assertArrayEquals ("failure - byte arrays not same" , expected , actual );
38+ }
39+
40+ @ Test
41+ public void testAssertEquals () {
42+ assertEquals ("failure - strings are not equal" , "text" , "text" );
43+ }
44+
45+ @ Test
46+ public void testAssertFalse () {
47+ assertFalse ("failure - should be false" , false );
48+ }
49+
50+ @ Test
51+ public void testAssertNotNull () {
52+ assertNotNull ("should not be null" , new Object ());
53+ }
54+
55+ @ Test
56+ public void testAssertNotSame () {
57+ assertNotSame ("should not be same Object" , new Object (), new Object ());
58+ }
59+
60+ @ Test
61+ public void testAssertNull () {
62+ assertNull ("should be null" , null );
63+ }
64+
65+ @ Test
66+ public void testAssertSame () {
67+ Integer aNumber = Integer .valueOf (768 );
68+ assertSame ("should be same" , aNumber , aNumber );
69+ }
70+
71+ // JUnit Matchers assertThat
72+ @ Test
73+ public void testAssertThatBothContainsString () {
74+ assertThat ("albumen" , both (containsString ("a" )).and (containsString ("b" )));
75+ }
76+
77+ @ Test
78+ public void testAssertThatHasItems () {
79+ assertThat (Arrays .asList ("one" , "two" , "three" ), hasItems ("one" , "three" ));
80+ }
81+
82+ @ Test
83+ public void testAssertThatEveryItemContainsString () {
84+ assertThat (Arrays .asList (new String [] { "fun" , "ban" , "net" }), everyItem (containsString ("n" )));
85+ }
86+
87+ // Core Hamcrest Matchers with assertThat
88+ @ Test
89+ public void testAssertThatHamcrestCoreMatchers () {
90+ assertThat ("good" , allOf (equalTo ("good" ), startsWith ("good" )));
91+ assertThat ("good" , not (allOf (equalTo ("bad" ), equalTo ("good" ))));
92+ assertThat ("good" , anyOf (equalTo ("bad" ), equalTo ("good" )));
93+ assertThat (7 , not (CombinableMatcher .<Integer > either (equalTo (3 )).or (equalTo (4 ))));
94+ assertThat (new Object (), not (sameInstance (new Object ())));
95+ }
96+
97+ @ Test
98+ public void testAssertTrue () {
99+ assertTrue ("failure - should be true" , true );
100+ }
101+ }
0 commit comments