Spock Framework Guide with Eclipse, Gradle, Groovy

Check “Learning resources” below used for this guide. Especially check this blog: http://thejavatar.com/

  1. Install Eclipse Luna from here
  2. Install Gradle Plugin, check it here
  3. Install Groovy-Eclipse for Juno or Indigo from Eclipse Marketplace (or maybe Groovy/Grails Tool Suite for Eclipse)
  4. groovy.png
  5. You will need to update this plugin to install other important things for Groovy: JDT Core and Groovy Feature
  6. Install Spock Plugin From Eclipse Marketplace if you want, check it here
  7. Import Project to Eclipse through Gradle Import
  8. Add these lines to build.gradle:
    1. apply plugin: ‘groovy’
    2. testCompile ‘org.spockframework:spock-spring:1.0-groovy-2.3’ (for Spring)
    3. this is quite important, version can make some conflicts
  9. Right Click on the project -> Add Spock Nature
  10. After this *.groovy and *.gradle files will problably looks different, Syntax colour highlightning etc. Remember that you can right click on for eg. build.gradle -> Open with -> Open With Minimalist gradle Editor etc.
  11. Probably you will need to make additional folder for *.groovy test files
  12. Create new *.groovy file, class
  13. Basic test example, extends Specification from Spock framework and needs specific Annotations when running with Spring
  14. spock.png
  15. Now you can run it with JUnit from Eclipse
  16. For integration tests you can’t use @RunWith(SpringJUnit4ClassRunner.class), and  Context should looks like here @ContextConfiguration(locations = [ “/restTestContext.xml” ]) , not {} braces, but  [ ]
  17. Spock can be used for Mocks too. Something like this: Subscriber subscriber1 = Mock() , subscriber1.isActive() >> true , So, remember >> operator for mocks.
  18. Learning/Reading resources:
    1. Docs
    2. Screencast – with awesome code templates
    3. Spring Boot & Spock
    4. Dependency for Spring from Maven Repository
    5. Nice looking Tutorial
    6. Spock Basics
    7. Spock Mock Cheatsheet
    8. Spock is awesome! Seriously Simplified Mocking
    9. Who is using Spock?
    10. Comparing Spock and JUnit
    11. http://edgibbs.com/spock-intro-a-bdd-testing-framework-in-groovy/
    12. Sample Presentation about Spock and why people like Spock:
      • Consist and readable test code
      • Tests as specification by default
      • All Groovy magic available to help
      • Embedded mocking framework (Although Mockito can be used if preferred)
      • Highly extensible
      • Compatible with tools supporting JUnit
  19. Comparison to JUnit

    Although Spock uses a different terminology, many of its concepts and features are inspired from JUnit. Here is a rough comparison:

    Spock JUnit
    Specification Test class
    setup() @Before
    cleanup() @After
    setupSpec() @BeforeClass
    cleanupSpec() @AfterClass
    Feature Test
    Parameterized feature Theory
    Condition Assertion
    Exception condition @Test(expected=...)
    @FailsWith @Test(expected=...)
    Interaction Mock expectation (EasyMock, JMock, …)
  20. My thoughts:

Pros:

  1. Test names are much easier to read, because you can use “space” key there
  2. I think less code is needed, easier to avoid huge amount of getters
  3. It is compatible with other frameworks, you can use it with JUnit, Spring, Mockito (I heard that, didn’t check it), Integration test available too.
  4. Parametrized tests looks quite cool, but didn’t tried it yet:
  5. param.png
  6. It’s Groovy, but not that much, still it looks nice. (btw. Groovy hotness bar can only go up)
  7. It seems that it can be a full solution for testing, other frameworks are not needed
  8. Seems to be Highly extensible and powerful

Cons:

  1. I like this framework, but it is a little bit annoying to use it with Eclipse… but maybe there is a way to make it more comfortable

PS. Spock Framework may work better with Spring Tool Suite (STS) or IntelliJ IDEA. Unfortunately it is quite easy to crash Eclipse. Too many plugins

Advertisements

2 thoughts on “Spock Framework Guide with Eclipse, Gradle, Groovy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s