Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test Easy and Prosper with Spock Framework

Test Easy and Prosper with Spock Framework

Introduction to what is unit testing and discussion why it is important. Presenting a cool framework for writing unit tests in Groovy that can be used testing any code written in JVM language.

Avatar for Tomche Delev

Tomche Delev

March 17, 2017
Tweet

More Decks by Tomche Delev

Other Decks in Programming

Transcript

  1. Agenda Why bother with unit testing? How to enjoy writing

    unit tests? Show me the code already!
  2. De nition A unit test is a piece of a

    code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. A "unit" is a method or function.
  3. HN Ok let's stop writing UT and see what happens.

    Wait... we've already tried that, and we know the result pretty well
  4. UT cannot assert the correctness of your code. But it

    will constructively assert its incorrectness.
  5. 8. Fowler: "Imperfect tests, run frequently, are much better than

    perfect tests that are never written at all"
  6. Then, after maybe one or two weeks, just as the

    soreness is going away, a Big Deadline begins approaching.
  7. The nal answer Unit Testing is usually worth the effort

    BUT the amount of effort required isn't going to be the same for everybody
  8. Spock is: Testing and speci cation framework for Java and

    Groovy applications With beautiful and highly expressive speci cation language Compatible with most IDEs, build tools, and continuous integration servers Inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms
  9. Why Spock? Reduce the lines of test code Make tests

    more readable Turn tests into speci cations Make testing fun again!
  10. Spock in the testing jungle Use Case Conventional Tool(s) A

    Single Tool Unit Testing JUnit TestNG Spock Mocking and Stubing EasyMock jMock Mockito PowerMock jMockit Behaviour Driven Design (BDD) Cucamber JBehave
  11. Comparison to JUnit Concepts JUnit Spock Test class Speci cation

    Test Feature Test method Feature method @Before setup() @After cleanup() Assertion Condition @Test(expected) Exception condition
  12. Hello Spock import spock.lang.Specification class StarshipSpec extends Specification { def

    "science officer of the starship Enterprise is Spock"() { given: def starship = new Starship("Enterprise", "Spock") when: def actual = starship.getScienceOfficer() then: actual == "Spock" } }