Check out this SlideShare Presentation:
View more presentations from Rafal Rusin.
import spock.lang.*
class UserSpec extends Specification {
}
Now we can proceed to defining test fixtures and test methods.class UserSpec extends Specification {
User user
Document document
def setup() {
user = new User()
document = DocumentTestFactory.createDocumentWithTitle("doc1")
}
def cleanup() {
}
}
Of course we can use field initialization for instantiating test objects:class UserSpec extends Specification {
User user = new User()
Document document = DocumentTestFactory.createDocumentWithTitle("doc1")
def setup() {
}
def cleanup() {
}
}
class UserSpec extends Specification {
// ...
def "should assign coment to user"() {
// ...
}
}
With such naming convention we can write real specification and include details about specified behaviour in method name, what is very convenient when reading test reports and analyzing errors.class UserSpec extends Specification {
// ...
def "should assign coment to user"() {
given:
// do initialization of test objects
when:
// perform actions to be tested
then:
// collect and analyze results
}
}
class UserSpec extends Specification {
// ...
def "should add project to user and mark user as project's owner"() {
given:
User user = new User()
Project project = ProjectTestFactory.createProjectWithName("simple project")
// ...
}
}
class UserSpec extends Specification {
// ...
def "should assign user to comment when adding comment to user"() {
given:
User user = new User()
Comment comment = new Comment()
when:
user.addComment(comment)
then:
comment.getUserWhoCreatedComment().equals(user)
}
// ...
}
user.getName() == "John"
user.getAge() == 40
!user.isEnabled()
Each of lines will be treated as single assertion and will be evaluated by Spock.class CommentSpec extends Specification {
def "should throw exception when adding null document to comment"() {
given:
Comment comment = new Comment()
when:
comment.setCommentedDocument(null)
then:
thrown(RuntimeException)
}
}
def "should create user with given name"() {
given:
User user = UserTestFactory.createUser("john doe")
expect:
user.getName() == "john doe"
}
def "should successfully validate emails with valid syntax"() {
expect:
emailValidator.validate(email) == true
where:
email }
def "should perform validation of email addresses"() {
expect:
emailValidator.validate(email) == result
where:
email result }
Well, it looks nice, but Spock can do much better. It offers tabular format of defining parameters for test what is much more readable and natural. Lets take a look:def "should perform validation of email addresses"() {
expect:
emailValidator.validate(email) == result
where:
email | result
"WTF" | false
"@domain" | false
"foo@bar.com" | true
"a@test" | false
}
In this code, each column of our "table" is treated as a separate variable and rows are values for subsequent test iterations.@Unroll("should validate email #email")
def "should perform validation of email addresses"() {
// ...
}
With that annotation, Spock generate few methods each with its own name and run them separately. We can use symbols from where blocks in @Unroll argument by preceding it with '#' sign what is a signal to Spock to use it in generated method name.Gradle promises to hit the sweet spot between Ant and Maven. It uses Ivy's approach for dependency resolution. It allows for convention over configuration but also includes Ant tasks as first class citizens. It also wisely allows you to use existing Maven/Ivy repositories.So why would one use yet another JVM build tool such as Gradle? The answer is simple: to avoid frustration involved by Ant or Maven.
<project root>Have I just referred myself for the 1st time? Achievement unlocked! ;)
│
├── build.gradle
└── src
├── main
│ ├── java
│ └── groovy
│
└── test
├── java
└── groovy
the-appthe-app itself has no src sub-folder as its purpose is only to contain sub-projects and build configuration. If needed it could've been provided with own src though.
│
├── dao-layer
│ └── src
│
├── domain-model
│ └── src
│
├── web-frontend
│ └── src
│
├── build.gradle
└── settings.gradle
include 'dao-layer', 'domain-model', 'web-frontend'Now the gradle projects command can be executed to obtain such a result:
:projects...so we know that Gradle noticed the modules. However gradle build command won't run successful yet because build.gradle file is still empty.
------------------------------------------------------------
Root project
------------------------------------------------------------
Root project 'the-app'
+--- Project ':dao-layer'
+--- Project ':domain-model'
\--- Project ':web-frontend'
apply plugin: 'java'This single line of config for any of modules is enough to execute gradle build command under the-app directory with following result:
:dao-layer:compileJavaTo use Groovy plugin slightly more configuration is needed:
:dao-layer:processResources UP-TO-DATE
:dao-layer:classes
:dao-layer:jar
:dao-layer:assemble
:dao-layer:compileTestJava UP-TO-DATE
:dao-layer:processTestResources UP-TO-DATE
:dao-layer:testClasses UP-TO-DATE
:dao-layer:test
:dao-layer:check
:dao-layer:build
BUILD SUCCESSFUL
Total time: 3.256 secs
apply plugin: 'groovy'At lines 3 to 6 Maven repositories are set. At line 9 dependency with groovy library version is specified. Of course plugin as 'java', 'groovy' and many more can be mixed each other.
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
groovy 'org.codehaus.groovy:groovy-all:2.0.5'
}
def langLevel = 1.7At the beginning simple variable langLevel is declared. It's worth knowing that we can use almost any Groovy code inside build.gradle file, statements like for example if conditions, for/while loops, closures, switch-case, etc... Quite an advantage over inflexible XML, isn't it?
allprojects {
apply plugin: 'idea'
group = 'com.tamashumi'
version = '0.1'
}
subprojects {
apply plugin: 'groovy'
sourceCompatibility = langLevel
targetCompatibility = langLevel
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
groovy 'org.codehaus.groovy:groovy-all:2.0.5'
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
}
}
project(':dao-layer') {
dependencies {
compile 'org.hibernate:hibernate-core:4.1.7.Final'
}
}
project(':domain-model') {
dependencies {
compile project(':dao-layer')
}
}
project(':web-frontend') {
apply plugin: 'war'
dependencies {
compile project(':domain-model')
compile 'org.springframework:spring-webmvc:3.1.2.RELEASE'
}
}
idea {
project {
jdkName = langLevel
languageLevel = langLevel
}
}