forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecordExampleTest.java.bk
More file actions
56 lines (45 loc) · 1.12 KB
/
RecordExampleTest.java.bk
File metadata and controls
56 lines (45 loc) · 1.12 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
package com.examplehub.basics;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class RecordExampleTest {
void test() {
record Point(int x, int y) {}
Point point = new Point(100, 200);
assertEquals(100, point.x);
assertEquals(100, point.x());
assertEquals(200, point.y());
assertEquals("Point[x=100, y=200]", point.toString());
}
void testConstructor() {
record Point(int x, int y) {
public Point {
if (x < 0 || y < 0) {
throw new IllegalArgumentException();
}
}
}
;
try {
Point point = new Point(-1, -1);
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
}
void testStaticMethod() {
record Point(int x, int y) {
public static Point of() {
return new Point(0, 0);
}
public static Point of(int x, int y) {
return new Point(x, y);
}
}
Point p1 = Point.of();
assertEquals(0, p1.x());
assertEquals(0, p1.y());
Point p2 = Point.of(100, 200);
assertEquals(100, p2.x());
assertEquals(200, p2.y());
}
}