diff --git a/core-java-12/pom.xml b/core-java-12/pom.xml index defef5e9d3..c5eec1a4bb 100644 --- a/core-java-12/pom.xml +++ b/core-java-12/pom.xml @@ -34,6 +34,7 @@ ${maven.compiler.source.version} ${maven.compiler.target.version} + --enable-preview diff --git a/core-java-12/src/test/java/com/baeldung/switchExpression/SwitchUnitTest.java b/core-java-12/src/test/java/com/baeldung/switchExpression/SwitchUnitTest.java new file mode 100644 index 0000000000..708e416090 --- /dev/null +++ b/core-java-12/src/test/java/com/baeldung/switchExpression/SwitchUnitTest.java @@ -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} +}