forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutEquality.java
More file actions
52 lines (43 loc) · 1.58 KB
/
Copy pathAboutEquality.java
File metadata and controls
52 lines (43 loc) · 1.58 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
package beginner;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutEquality {
@Koan
public void doubleEqualsTestsIfTwoObjectsAreTheSame() {
Object object = new Object();
Object sameObject = object;
assertEquals(object == sameObject, __);
assertEquals(object == new Object(), __);
}
@Koan
public void equalsMethodByDefaultTestsIfTwoObjectsAreTheSame() {
Object object = new Object();
assertEquals(object.equals(object), __);
assertEquals(object.equals(new Object()), __);
}
@Koan
public void equalsMethodCanBeChangedBySubclassesToTestsIfTwoObjectsAreEqual() {
Object object = new Integer(1);
assertEquals(object.equals(object), __);
assertEquals(object.equals(new Integer(1)), __);
// Note: This means that for the class 'Object' there is no difference between 'equal' and 'same'
// but for the class 'Integer' there is difference - see below
}
@Koan
public void equalsMethodCanBeChangedBySubclassesToTestsIfTwoObjectsAreEqualExample() {
Integer value1 = new Integer(4);
Integer value2 = new Integer(2 + 2);
assertEquals(value1.equals(value2), __);
assertEquals(value1, __);
}
@Koan
public void objectsNeverEqualNull() {
assertEquals(new Object().equals(null), __);
}
@Koan
public void objectsEqualThemselves() {
Object obj = new Object();
assertEquals(obj.equals(obj), __);
}
}