CategoriesTesting

Spock Test Framework

Spock aims to be a more powerful alternative to the traditional JUnit stack, by leveraging Groovy features which seamlessly integrate with Java

Spock Features Highlights

  • Highly Expressive
  • Spock enforces a clear test structure
  • BDD style test can be expressive through the “given, when and then” blocks
  • Compatible with all major build tools and continuous integration providers
  • Inbuilt Mocking and Stubbing
  • Parameterized test and Data-driven test ( Data table-based and SQL table-based)

JUNIT -> SPOCK -> CUCUMBER

Spock lies between junit and cucumber

At a glance, the Spock framework is,

  • BDD style testing and specification framework
  • Which runs Junit under the hood
  • For Java and Groovy applications
  • Test case writing in Groovy
  • All three components combine with a single library
    • Unit testing
    • Mocking
    • BDD

given, when, then, expect, cleanup, and where

  • Given – any setup needed before a test is run.
  • When – This is where we provide a stimulus to what is under test. In other words, where we invoke our method under test
  • Then – This is where the assertions belong. In Spock, these are evaluated as plain Boolean assertions
  • Expect – This is a way of performing our stimulus and assertion within the same block.
  • Cleanup – Here we tear down any test dependency resources that would otherwise be left behind.

Some highlights

  • A Specification,
    • is represented as a Groovy class that extends from “spock.lang.Specification”. 
    • The name of a specification usually relates to the system or system operation described by the specification.
  • Fixture Methods
    • def setupSpec() {}    // runs once –  before the first feature method
    • def setup() {}        // runs before every feature method
    • def cleanup() {}      // runs after every feature method
    • def cleanupSpec() {}  // runs once –  after the last feature method

Original documentation provides more details with all kinds of examples, Some highlights are shown below,

Let’s try more specific example with compared to other test frameworks

JUNIT -> SPOCK -> CUCUMBER

Let’s try a real-world scenario with all three formats of testing.

Use case: Event Routing

X application consumes messages from many systems and based on the Message content, it will be routed to the target systems

  • Messages are consumed from
    • RIC, RT, RL 
  • Routed to multiple Targets
    • EUCLID, XGIX, RL, KIWI, RIC

/TODO add coding examples

Spock With Spring Framework

  • Similar to Junit spring testing
  • The framework supports all types of spring test annotations
    • @Autowired
    • @ContextConfiguration
    • @SpringBootTest
    • @SpringMVCTest
  • Mocks
    • DetachedMockFactory – allows the creation of Spock mocks outside of a specification
    • @SpringBean – Similar to spring @MockBean
    • @SpringSpy – If you want to spy on an existing bean

More details are available here

Leave a Reply

Your email address will not be published. Required fields are marked *