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

By

Agenda


  • Who is this new Indian guy?

  • BDD

    • Why? What? How?

  • Spock Framework

    • Why? What? How?
  • Demo

  • No Questions Please..

Who Am I?



Dhiraj Mahapatro

Grails Developer @ NetJets

twitter # @dhirajmahapatro

GitHub # dmahapatro

Stackoverflow # dmahapatro

Why BDD when we follow TDD?

TDD facilitates writing code correct

BDD facilitates writing correct code

TDD Approach



@TestFor(SampleController)
@Mock(Sample)
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?

Never tested

  • Do we really need "Hello World" back from action?
  • Did we actually meet the acceptance criteria?
  • Expected Behavior of the application?

Result >>

Unhappy BA

Solution

Be Cognizant of the behavior [3 Amigos]

Development based on Behavior

How to test in Grails/Groovy/Java?

Introducing


Spock Framework

Creator
Peter Niederwieser

twitter # @pniedrw

Behavior Based Test Case



@TestFor(SampleController)
@Mock(Sample)
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

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


Sources

@Spock Framework Docs: 

http://docs.spockframework.org/en/spock-0.7-groovy-2.0/introduction.html

@BDD Definition:

http://dannorth.net/introducing-bdd/

@Google Code:

https://code.google.com/p/spock/

@Github

https://github.com/spockframework/spock


Thank You