Skip to content

Commit 871ee3c

Browse files
rodrigolgracianopivovarit
authored andcommitted
BAEL-2844 (eugenp#6641)
1 parent e4c8afa commit 871ee3c

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎core-java-12/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<configuration>
3535
<source>${maven.compiler.source.version}</source>
3636
<target>${maven.compiler.target.version}</target>
37+
<compilerArgs>--enable-preview</compilerArgs>
3738
</configuration>
3839
</plugin>
3940
</plugins>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.switchExpression;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class SwitchUnitTest {
7+
8+
@Test
9+
public void switchJava12(){
10+
11+
var month = Month.AUG;
12+
13+
var value = switch(month){
14+
case JAN,JUN, JUL -> 3;
15+
case FEB,SEP, OCT, NOV, DEC -> 1;
16+
case MAR,MAY, APR, AUG -> 2;
17+
};
18+
19+
Assert.assertEquals(value, 2);
20+
}
21+
22+
@Test
23+
public void switchLocalVariable(){
24+
var month = Month.AUG;
25+
int i = switch (month){
26+
case JAN,JUN, JUL -> 3;
27+
case FEB,SEP, OCT, NOV, DEC -> 1;
28+
case MAR,MAY, APR, AUG -> {
29+
int j = month.toString().length() * 4;
30+
break j;
31+
}
32+
};
33+
Assert.assertEquals(12, i);
34+
}
35+
36+
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
37+
}

0 commit comments

Comments
 (0)