diff --git a/core-groovy-2/README.md b/core-groovy-2/README.md index 9701976142..f60bdb3cbe 100644 --- a/core-groovy-2/README.md +++ b/core-groovy-2/README.md @@ -2,4 +2,6 @@ ## Relevant articles: -- [String Matching in Groovy](http://www.baeldung.com/) \ No newline at end of file +- [String Matching in Groovy](http://www.baeldung.com/) +- [Groovy def Keyword] + diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index eb82f5d2e3..77de9c8fc8 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -14,12 +14,33 @@ + + org.codehaus.groovy + groovy + ${groovy.version} + org.codehaus.groovy groovy-all ${groovy-all.version} pom + + org.codehaus.groovy + groovy-dateutil + ${groovy.version} + + + org.codehaus.groovy + groovy-sql + ${groovy-sql.version} + + + org.junit.platform + junit-platform-runner + ${junit.platform.version} + test + org.hsqldb hsqldb @@ -43,12 +64,39 @@ + addSources + addTestSources compile compileTests + + maven-failsafe-plugin + ${maven-failsafe-plugin.version} + + + org.junit.platform + junit-platform-surefire-provider + ${junit.platform.version} + + + + + junit5 + + integration-test + verify + + + + **/*Test5.java + + + + + maven-surefire-plugin 2.20.1 @@ -71,10 +119,13 @@ + 1.0.0 + 2.5.6 2.5.6 + 2.5.6 2.4.0 - 1.3-groovy-2.5 - 1.6.3 + 1.1-groovy-2.4 + 1.6 diff --git a/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy new file mode 100644 index 0000000000..310d97d3fd --- /dev/null +++ b/core-groovy-2/src/test/groovy/com/baeldung/defkeyword/DefUnitTest.groovy @@ -0,0 +1,79 @@ +package com.baeldung.defkeyword + +import org.codehaus.groovy.runtime.NullObject +import org.codehaus.groovy.runtime.typehandling.GroovyCastException + +import groovy.transform.TypeChecked +import groovy.transform.TypeCheckingMode + +@TypeChecked +class DefUnitTest extends GroovyTestCase { + + def id + def firstName = "Samwell" + def listOfCountries = ['USA', 'UK', 'FRANCE', 'INDIA'] + + @TypeChecked(TypeCheckingMode.SKIP) + def multiply(x, y) { + return x*y + } + + @TypeChecked(TypeCheckingMode.SKIP) + void testDefVariableDeclaration() { + + def list + assert list.getClass() == org.codehaus.groovy.runtime.NullObject + assert list.is(null) + + list = [1,2,4] + assert list instanceof ArrayList + } + + @TypeChecked(TypeCheckingMode.SKIP) + void testTypeVariables() { + int rate = 200 + try { + rate = [12] //GroovyCastException + rate = "nill" //GroovyCastException + } catch(GroovyCastException) { + println "Cannot assign anything other than integer" + } + } + + @TypeChecked(TypeCheckingMode.SKIP) + void testDefVariableMultipleAssignment() { + def rate + assert rate == null + assert rate.getClass() == org.codehaus.groovy.runtime.NullObject + + rate = 12 + assert rate instanceof Integer + + rate = "Not Available" + assert rate instanceof String + + rate = [1, 4] + assert rate instanceof List + + assert divide(12, 3) instanceof BigDecimal + assert divide(1, 0) instanceof String + + } + + def divide(int x, int y) { + if(y==0) { + return "Should not divide by 0" + } else { + return x/y + } + } + + def greetMsg() { + println "Hello! I am Groovy" + } + + void testDefVsType() { + def int count + assert count instanceof Integer + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2e31200652..0de4a36336 100644 --- a/pom.xml +++ b/pom.xml @@ -376,7 +376,7 @@ cdi checker-plugin core-groovy - core-groovy-2 + core-groovy-2