Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Integration Testing with GSpec

If you prefer GSpec is an evolving framework to allow you to apply a BDD style of programming you might decide to use GSpec.Here is what you might end up withwhen using Groovy. The sections below illustrate using GSpec for the examples from Using Testing Frameworks with Groovy.

The Stack Example

Here is how you might test the Item Storer Example with GSpec:

Code Block

import com.craig.gspec.GSpecBuilderRunner

def the = new GSpecBuilderRunner()

the.context('A non-empty stack') {
    initially {
        the.stack = new FixedStack()
        ('a'..'c').each { x -> the.stack.push x }
        the.stack.should_not_be_empty
    }

    specify('should return the top item when sent #peek') {
        the.stack.peek().should_equal 'c'
    }

    specify('should NOT remove the top item when sent #peek') {
        the.stack.peek().should_equal 'c'
        the.stack.peek().should_equal 'c'
    }

    specify('should be unchanged when sent #push then #pop') {
        the.stack.push 'Anything'
        the.stack.pop()
        the.stack.peek().should_equal 'c'
    }

    specify('should return the top item when sent #pop') {
        the.stack.pop().should_equal 'c'
        the.stack.push 'c' // put it back the way it was
    }

    specify('should remove the top item when sent #pop') {
        the.stack.pop().should_equal 'c'
        the.stack.pop().should_equal 'b'
    }

    specify('should add to the top when sent #push') {
        the.stack.push 'd'
        the.stack.peek().should_equal 'd'
    }
}

the.context('An empty stack') {
    initially {
        the.stack = new FixedStack()
        the.stack.should_be_empty
    }

    specify('should no longer be empty after #push') {
        the.stack.push 'anything'
        the.stack.should_not_be_empty
    }

    specify('should complain when sent #peek') {
//        the.stack.peek().should_fail_with(StackUnderflowException)
    }

    specify('should complain when sent #pop') {
//        the.stack.pop().should_fail_with(StackUnderflowException)
    }
}

the.context('A stack with one item') {
    initially {
        the.stack = new FixedStack()
        the.stack.push 'anything'
        the.stack.should_not_be_empty
    }

    specify('should remain not empty after #peek') {
        the.stack.peek()
        the.stack.should_not_be_empty
    }

    specify('should become empty after #pop') {
        the.stack.pop()
        the.stack.should_be_empty
    }
}

the.context('A stack with one item less than capacity') {
    initially {
        the.stack = new FixedStack()
        (1..<FixedStack.MAXSIZE).each { x -> the.stack.push x }
        the.stack.should_not_be_full
    }

    specify('should become full after #push') {
        the.stack.push 'Anything'
        the.stack.should_be_full
    }
}

the.context('A full stack') {
    initially {
        the.stack = new FixedStack()
        (1..FixedStack.MAXSIZE).each { x -> the.stack.push x }
        the.stack.should_be_full
    }

    specify('should remain full after #peek') {
        the.stack.peek()
        the.stack.should_be_full
    }

    specify('should no longer be full after #pop') {
        the.stack.pop()
        the.stack.should_not_be_full
    }

    specify('should complain on #push') {
//        the.stack.push('Anything').should_fail_with(StackOverflowException)
    }
}

The Item Storer Example

Here is how you might integration test the Item Storer Example with GSpec:

Code Block
import com.craig.gspec.GSpecBuilderRunner

def checkPersistAndReverse(storer, orig, reversed){
    storer.put orig
    storer.get().should_equal orig
    storer.getReverse().should_equal reversed
}

def the = new GSpecBuilderRunner()

the.context('A new storer') {
    initially() {
        the.storer = new Storer()
    }
    specify('Should persist and reverse strings') {
        checkPersistAndReverse the.storer, 'hello', 'olleh'
    }
    specify('Should persist and reverse numbers') {
        checkPersistAndReverse the.storer, 123.456, -123.456
    }
    specify('Should persist and reverse lists') {
        checkPersistAndReverse the.storer, [1, 3, 5], [5, 3, 1]
    }
}