This commit is contained in:
Rodrigo Graciano 2019-04-03 13:20:28 -04:00 committed by Grzegorz Piwowarek
parent e4c8afab7a
commit 871ee3ccce
2 changed files with 38 additions and 0 deletions

View File

@ -34,6 +34,7 @@
<configuration>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
</plugins>

View File

@ -0,0 +1,37 @@
package com.baeldung.switchExpression;
import org.junit.Assert;
import org.junit.Test;
public class SwitchUnitTest {
@Test
public void switchJava12(){
var month = Month.AUG;
var value = switch(month){
case JAN,JUN, JUL -> 3;
case FEB,SEP, OCT, NOV, DEC -> 1;
case MAR,MAY, APR, AUG -> 2;
};
Assert.assertEquals(value, 2);
}
@Test
public void switchLocalVariable(){
var month = Month.AUG;
int i = switch (month){
case JAN,JUN, JUL -> 3;
case FEB,SEP, OCT, NOV, DEC -> 1;
case MAR,MAY, APR, AUG -> {
int j = month.toString().length() * 4;
break j;
}
};
Assert.assertEquals(12, i);
}
enum Month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}
}