Skip to content

Commit 89a3736

Browse files
committed
Added examples for assert section
1 parent 7aebe35 commit 89a3736

File tree

8 files changed

+156
-2
lines changed

8 files changed

+156
-2
lines changed

.idea/compiler.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/copyright/profiles_settings.xml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SampleApp/.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

SampleApp/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.0-rc1'
8+
classpath 'com.android.tools.build:gradle:2.2.0-rc2'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

0 commit comments

Comments
 (0)