[B]ehavior [D]riven [D]evelopment – Development based on Behavior



[B]ehavior [D]riven [D]evelopment – Development based on Behavior

1 0


bdd-grails-spock-presentation

Presentation on BDD and Spock Framework

On Github dmahapatro / bdd-grails-spock-presentation

[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

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

Data Driven Interaction Based

Sources

@Spock Framework Docs: 

@BDD Definition:

@Google Code:

@Github

Thank You