• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Informational: Spock versus JUnit

 
Author
Posts: 16
5
Spring VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all

While this week there is a promotion for the Spock book by Manning I would like to offer some more insight on the what the book covers and the mindset behind its content.
Also since I started writing the book I have gathered a lot of questions from readers so I have a good feeling on some of the first questions you are thinking when learning about Spock.

Now first things first

If you are a seasoned Java developer and know your way around JUnit the best short resource would be my presentation the directly compares JUnit and Spock
It is in PDF format here http://codepipes.com/presentations/spock-vs-junit.pdf

If you want the full story on JUnit versus Spock, the best resource would be Chapter 3 of the book. It is freely available in PDF format as well
https://manning-content.s3.amazonaws.com/download/3/d90f946-9baf-46eb-859c-fb7a56608320/SampleChapter-03.pdf

The chapter contains some almost-realistic examples and compares what happens with JUnit and Spock (and why Spock is better)

If you are a Java beginner or don't know too much about testing frameworks, then Chapter 1 is a very gentle introduction to testing with Spock. This chapter is also available for free
https://manning-content.s3.amazonaws.com/download/e/82672af-9245-44a7-9542-a9855972700c/SampleChapter-01.pdf

Now here is a small FAQ for Spock (one question in the forum is already contained in the FAQ)

The creators of Spock knew that JUnit is very well entrenched in the Java ecosystem and therefore when they created Spock they made the smart decision to use the JUnit runner.
The JUnit runner (without getting into many details) is the way other tools run JUnit tests. When you run a JUnit test from Eclipse, Intellij, Maven, Gradle etc you call the JUnit runner.

Spock is using the JUnit runner internally instead of introducing a completely new way of running tests. This has the effect that all Spock tests "appear" as normal JUnit tests to existing tools.
This means that the answer to the following questions:

  • How do I include Spock tests in my project?
  • How do I run Spock tests?
  • How do I debug Spock tests?
  • How do I get code Coverage?
  • How do I integrate with Sonar?


  • ...is the same: You did the same things you did before as with JUnit

    If you run JUnit tests by right clicking on the IDE, you can run Spock tests by right clicking on the IDE
    If you run JUnit tests via Gradle, you can run Spock tests via Gradle
    If you use Sonar for code coverage in JUnit tests, you still use Sonar for code coverage in Spock tests.

    On the other hand with Spock you gain

  • Less code (because Groovy is more concise than Java)
  • Mocking and Stubbing for free with no need for an external library
  • Code blocks to organize semantically you unit tests
  • Data pipes, tables and generators for parameterized tests

  • and many more features that are described in the book

    Kostis
     
    reply
      Bookmark Topic Watch Topic
    • New Topic