Groovy: Why Spock?
Reasons to use Spock
- a few hours to learn and implement Spock tests to business grade applications
- extremely easy to create test, add test cases, refactor, extend
- quick execution of test suites
- lightweight
- applicable to unit tests, IT, E2E, BDD
More advantages
- gives clear details where test fails
- readible methods naming (natural language description)
- readible given / when / then blocks, often with English didascalia (BDD)
- can be used along with Java jUnit, Python, Cucumber / Gherkin within the same project
Characteristics
- testing framework
- runs on JUnit runner
- can mock and stub - like Mockito - but it’s included
- embraces the paradigm of behavior-driven development (BDD)
- given / when / then declarative style of BDD
- no assert statemets, no assert methods required (but can use assert keyword -> Groovy truth)
- see https://meetspock.appspot.com/
- see https://groovyconsole.appspot.com/
- Spock is Groovy language testing Java code
- but the reverse is possible: JUnit Java testing Groovy code , JUnit Groovy (sic!) testing Groovy OR Java code
- Groovy supports a GroovyTestCase class, which extends the standard TestCase from JUnit
Requirements
- space to be created in the classpath (groovy test directory tree in project structure hierarchy)
- directory marked as Test Sources Root in Intellij IDEA
- Groovy & Spock dependency to be added to Maven, compatible with JDK / Spring version
- configured Maven Failsafe or Surefire plugin to run Spock tests (specifications) during Maven build (mvn clean install)
- sometimes configuration of CI / CD infrastructure (like Jenkins or Bamboo) is needed to run Spock Specs during build / deployment accordingly to project requirements
Conclusions
- fits business-level applications (also corporate, industry, everything…)
- no overhead, easy to learn, implement and use
- unit testing: imho best existing solution
- IT, E2E, BDD: interesting & promising alternative to other options
- modern technology, low risk of technical debt
References
- official website
- Spock Up and Running. Writing Expressive Tests in Java and Groovy, Rob Fletcher, O’Reilly, 2017
- Java Testing With Spock, Konstantinos Kapelonis, Manning, 2016