-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstanceOfTest.java
More file actions
27 lines (22 loc) · 912 Bytes
/
InstanceOfTest.java
File metadata and controls
27 lines (22 loc) · 912 Bytes
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
package com.github.hubertwo.playground.java16.instance;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Fail.fail;
class InstanceOfTest {
@Test
@DisplayName("JEP 394: Pattern Matching for instanceof")
public void patternMatchingForInstanceOf() {
Object givenObject = "Let's guess the type";
// Check instanceOf and assign to variable in single line
if (givenObject instanceof Integer actualObject) {
assertThat(actualObject).isInstanceOf(String.class);
} else if (givenObject instanceof String actualObject) {
assertThat(actualObject)
.isInstanceOf(String.class)
.isEqualTo(givenObject.toString());
} else {
fail("No instance of match!");
}
}
}