[B]ehavior [D]riven [D]evelopment



  • Who is this new Indian guy?
  • BDD
    • Why? What? How?
  • Spock Framework
    • Why? What? How?
  • Demos
  • No Questions Please.. 

Who Am I?

Dhiraj Mahapatro

Grails Developer @ NetJets
twitter # @dhirajmahapatro
Stackoverflow# dmahapatro

BDD facilitates writing correct code

Why BDD when we follow TDD? 

TDD facilitates writing code correct

TDD Approach

class SampleControllerTests {
    void testControllerActionShowReturnsString() {       //Create mocked object instance
       new Sample(name: 'Sample')

//Call controller action controller.show()
//Assert response        assert controller.response.contentAsString == 'Hello World'     } }
Test makes sure the controller action is called and response is got back appropriately.

We tested:

  • Code works correctly!!! [Peace]
  • Unit test passes!!!
  • Who cares about business acceptance criteria?

But we never tested:

  • Do we really want "Hello World" back from action?
  • Did we actually meet the acceptance criteria?
  • The behavior of the application?


Unhappy BA


Be Cognizant of the behavior [3 Amigos]

Development based on Behavior

Behavior Based Test Case

class SampleControllerTests extends ControllerSpec{
    void "test controller should return Grails and Ale"() {       given: "A sample object with name Sample"          new Sample(name: 'Sample')
              when: "Controller show action is called"          controller.show()

then: "The response should be Grails and Ale"
        controller.response.contentAsString == 'Grails and Ale'     } }
Self documented test matching laid out specification

How to do that in Grails or Groovy or Java?


Spock Framework

Peter Niederwieser

twitter # @pniedrw

Address Pain Points

  • Adhere to BDD
  • Easy mocking API
  • Easy Stubbing API
  • Fine Grained testing
  • Above all, its Groovier.

Types of Testing

  1. Data Driven
  2. Interaction Based


@Spock Framework Docs: 

@BDD Definition: 

@Google Code: 


Thank You


By Dhiraj Mahapatro


Presentation on BDD by Spock Framework

  • 745
Loading comments...

More from Dhiraj Mahapatro