Check out this SlideShare Presentation:
View more presentations from Rafal Rusin.
Couple of years ago I wasn't a big fan of unit testing. It was obvious to me that well prepared unit tests are crucial though. I didn't known why exactly crucial yet then. I just felt they are important. My disliking to write automation tests was mostly related to the effort necessary to prepare them. Also a spaghetti code was easily spotted in test sources.apply plugin: 'groovy'As you can see the build.gradle file is almost self-explanatory. Groovy plugin is applied to compile groovy code. It needs groovy-all.jar - declared in version 2.0 at dependencies block just next to Spock in version 0.7. What's most important, mentioned Maven repository URL is added at repositories block.
apply plugin: 'idea'
def langLevel = 1.7
sourceCompatibility = langLevel
targetCompatibility = langLevel
group = 'com.tamashumi.example.testwithspock'
version = '0.1'
repositories {
mavenLocal()
mavenCentral()
maven { url 'http://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
groovy 'org.codehaus.groovy:groovy-all:2.0.1'
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0-SNAPSHOT'
}
idea {
project {
jdkName = langLevel
languageLevel = langLevel
}
}
<project root>To build a project now you can type command gradle build or gradle test to only run tests.
│
├── build.gradle
└── src
├── main
│ ├── groovy
└── test
└── groovy
apply plugin: 'java'This way if you don't want or just can't deploy Groovy compiled stuff into your production JVM for any reason, still whole goodness of testing with Spock and Groovy is at your hand.
public class SimpleJavaClass {
public int sumAll(int... args) {
int sum = 0;
for (int arg : args){
sum += arg;
}
return sum;
}
}class SimpleGroovyClass {
String concatenateAll(char separator, String... args) {
args.join(separator as String)
}
}class JustASpecification extends Specification {
@Unroll('Sums integers #integers into: #expectedResult')
def "Can sum different amount of integers"() {
given:
def instance = new SimpleJavaClass()
when:
def result = instance.sumAll(* integers)
then:
result == expectedResult
where:
expectedResult | integers
11 | [3, 3, 5]
8 | [3, 5]
254 | [2, 4, 8, 16, 32, 64, 128]
22 | [7, 5, 6, 2, 2]
}
@Unroll('Concatenates strings #strings with separator "#separator" into: #expectedResult')
def "Can concatenate different amount of integers with a specified separator"() {
given:
def instance = new SimpleGroovyClass()
when:
def result = instance.concatenateAll(separator, * strings)
then:
result == expectedResult
where:
expectedResult | separator | strings
'Whasup dude?' | ' ' as char | ['Whasup', 'dude?']
'2012/09/15' | '/' as char | ['2012', '09', '15']
'nice-to-meet-you' | '-' as char | ['nice', 'to', 'meet', 'you']
}
} To run tests with Gradle simply execute command gradle test. Test reports can be found at <project root>/build/reports/tests/index.html and look kind a like this.apply plugin: 'war'in the build.gradle file and create a directory src/main/webapp.
apply plugin: 'groovy'
apply plugin: 'idea'
def langLevel = 1.7
sourceCompatibility = langLevel
targetCompatibility = langLevel
group = 'com.tamashumi.example.testwithspock'
version = '0.1'
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
groovy 'org.codehaus.groovy:groovy-all:2.0.5'
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
}
idea {
project {
jdkName = langLevel
languageLevel = langLevel
}
}