Repository navigation
Expand file tree
/
Copy pathAboutAssertions.java
More file actions
executable file
·78 lines (65 loc) · 2.1 KB
/
Copy pathAboutAssertions.java
File metadata and controls
executable file
·78 lines (65 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package beginner;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.*;
public class AboutAssertions {
@Koan
public void assertBooleanTrue() {
// there are two possibilities, true or false, what would it be here?
assertTrue(true);
}
@Koan
public void assertBooleanFalse() {
assertFalse(false);
}
@Koan
public void assertNullObject() {
// reference to the object can be null, a magic keyword, null, which means
// that there is nothing there
assertNull(null);
}
@Koan
public void assertNullObjectReference() {
Object someObject = null;
assertNull(someObject);
}
@Koan
public void assertNotNullObject() {
// but what when there should not be a null value?
assertNotNull(true);
}
@Koan
public void assertEqualsUsingExpression() {
assertTrue("Hello World!".equals("Hello World!"));
}
@Koan
public void assertEqualsWithAFewExpressions() {
assertEquals("Hello World!", "Hello World!");
assertEquals(1, 1);
assertEquals(2 + 2, 4);
assertEquals(2 * 3, 6);
assertEquals(3 - 8, -5);
assertEquals(10 / 2, 5);
}
@Koan
public void assertEqualsWithDescriptiveMessage() {
// Generally, when using an assertXXX methods, expectation is on the
// left and it is best practice to use a String for the first arg
// indication what has failed
assertEquals("The answer to 'life the universe and everything' should be 42", 42, 42);
}
@Koan
public void assertSameInstance() {
// Just because something is equal doesn't mean that it is the same.
// It's only the same if the reference is the same.
Object same = new Integer(1);
Object sameReference = same;
assertSame(same, sameReference);
}
@Koan
public void assertNotSameInstance() {
Integer same = new Integer(1);
Integer sameReference = 1;
assertNotSame(same, sameReference);
}
}