Merge pull request #6727 from SmartyAnsh/master

BAEL-2661 - Groovy def keyword
This commit is contained in:
Eric Martin 2019-04-20 19:05:30 -05:00 committed by GitHub
commit b2b80c89e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 136 additions and 4 deletions

View File

@ -3,3 +3,5 @@
## Relevant articles:
- [String Matching in Groovy](http://www.baeldung.com/)
- [Groovy def Keyword]

View File

@ -14,12 +14,33 @@
</parent>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-all.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-dateutil</artifactId>
<version>${groovy.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-sql</artifactId>
<version>${groovy-sql.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
@ -43,12 +64,39 @@
<executions>
<execution>
<goals>
<goal>addSources</goal>
<goal>addTestSources</goal>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>junit5</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*Test5.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
@ -71,10 +119,13 @@
</repositories>
<properties>
<junit.platform.version>1.0.0</junit.platform.version>
<groovy.version>2.5.6</groovy.version>
<groovy-all.version>2.5.6</groovy-all.version>
<groovy-sql.version>2.5.6</groovy-sql.version>
<hsqldb.version>2.4.0</hsqldb.version>
<spock-core.version>1.3-groovy-2.5</spock-core.version>
<gmavenplus-plugin.version>1.6.3</gmavenplus-plugin.version>
<spock-core.version>1.1-groovy-2.4</spock-core.version>
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
</properties>
</project>

View File

@ -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
}
}