Build.gradle
============
plugins {
id 'groovy'
id 'jacoco'
}
repositories {
jcenter()
}
dependencies {
implementation 'org.codehaus.groovy:groovy:2.5.9'
implementation 'org.codehaus.groovy:groovy-json:2.5.9'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
}
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination file("${reportsDir}/jacoco")
}
}
============= Main class ====
import java.util.concurrent.CompletableFuture
public class GroovyClosureMain {
def String method1(def name) {
def name1 = 'Hello ' + name
def text = ' - Closure Test'
CompletableFuture<Object> future = callAsync(name1, text)
return future.get()
}
def CompletableFuture<Object> callAsync(arg1, arg2) {
return CompletableFuture.supplyAsync({ return arg1 + ' ' + arg2 })
}
}
=============
Test Class
import spock.lang.Specification
class GroovyClosureTest extends Specification{
def "Test Closure1" () {
setup:
GroovyClosureMain mainCls = new GroovyClosureMain()
String name = "Name"
when:
String response = mainCls.method1(name)
then:
assert response != null
}
}
