JUnit
RSpec
Cucumber
jMock
Grails 2.3.x
Readability
Parametrization
Crazy Awesome Mocking
Extensible
class OrderServiceSpec extends Specification { ... }any Java or Groovy class that extends Specification
class OrderServiceSpec extends Specification { def setup() { ... } def cleanup() { ... } def setupSpec() { ... } def cleanupSpec() { ... } }
class OrderServiceSpec extends Specification { def "this is an actual test"() { ... } }
class OrderServiceSpec extends Specification { def "this is an actual test"() { setup: given: when: then: expect: where: cleanup: and: } }
@TestFor(OrderService) class OrderServiceTests { def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void calculateOrderForOneBeer() { mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) BigDecimal total = service.checkout(order) assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceTests extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void calculateOrderForOneBeer() { mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) BigDecimal total = service.checkout(order) assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void calculateOrderForOneBeer() { mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) BigDecimal total = service.checkout(order) assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) BigDecimal total = service.checkout(order) assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) BigDecimal total = service.checkout(order) assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: BigDecimal total = service.checkout(order) assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: BigDecimal total = service.checkout(order) then: assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: BigDecimal total = service.checkout(order) then: assert total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: BigDecimal total = service.checkout(order) then: total == 3.50 assertEquals(total, 3.50) mockInventoryService.verify() == null } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: BigDecimal total = service.checkout(order) mockInventoryService.verify() then: total == 3.50 assertEquals(total, 3.50) } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies and create valid order" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies and create valid order" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
void "add 2 things together" () { expect: 1 + 1 = 2 'Bat' + 'man' == 'Bat man' 99 + 1 == 100 }
--Output from test adding things-- | Failure: test adding things | Condition not satisfied: 'Bat' + 'man' == 'Bat man' | | Batman false 1 difference (85% similarity) Bat(-)man Bat( )man
void "adding things"() { expect: a + b == c where: a << [1, 'Bat', 100] b << [1, 'man', 1] c << [2, 'Bat man', 101] }
@Unroll void "adding things"() { expect: a + b == c where: a << [1, 'Bat', 100] b << [1, 'man', 1] c << [2, 'Bat man', 101] }
@Unroll("#a + #b = #c") void "adding things"() { expect: a + b == c where: a << [1, 'Bat', 100] b << [1, 'man', 1] c << [2, 'Bat man', 101] }
@Unroll void "#a + #b = #c"() { expect: a + b == c where: a << [1, 'Bat', 100] b << [1, 'man', 1] c << [2, 'Bat man', 101] }
@Unroll void "#a + #b = #c"() { expect: a + b == c where: a << [1, 'Bat', 100] b << [1, 'man', 1] c << [2, 'Bat man', 101] }
@Unroll void "#a + #b = #c"() { expect: a + b == c where: a << [1, 'Bat', 100, "Foo"] b << [1, 'man', 1, 1] c << [2, 'Bat man', 101, Foo1] }
@Unroll void "#a + #b = #c"() { expect: a + b == c where: a << [1, 'Bat', 100, "Foo", "2"] b << [1, 'man', 1, 1, "99"] c << [2, 'Bat man', 101, Foo1, "299"] }
@Unroll void "#a + #b = #c"() { expect: a + b == c where: a | b | c variables 1 | 1 | 2 'Bat' | 'man' | 'Bat man' Tests 100 | 1 | 101 }
@Unroll void "#a + #b = #c"() { expect: a + b == c where: a | b || c 1 | 1 || 2 'Bat' | 'man' || 'Bat man' 100 | 1 || 101 }
void "should throw IllegalArgumentException"() { when: service.doFoo('bad param') then: thrown(IllegalArgumentException) }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ def mockInventoryService def mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCostService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = mockFor(InventoryService) service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = mockFor(ShippingCalculatorService) service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService.createMock() mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService.createMock() } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.demand.removeFromStock(2) { a -> } mockShippingCostService.demand.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" mockInventoryService.removeFromStock(2) { a -> } mockShippingCostService.calculate(1) { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 2 * mockInventoryService.removeFromStock() { a -> } 1 * mockShippingCostService.calculate() { Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 2 * mockInventoryService.removeFromStock() { a -> } 1 * mockShippingCostService.calculate(){ Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 2 * mockInventoryService.removeFromStock(_) 1 * mockShippingCostService.calculate(){ Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 2 * mockInventoryService.removeFromStock(_) 1 * mockShippingCostService.calculate(){ Order order -> 1.00} and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 2 * mockInventoryService.removeFromStock(_) 1 * mockShippingCostService.calculate(_ as Order) >> 1.00 and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 1 * mockInventoryService.removeFromStock(_) 1 * mockShippingCostService.calculate(_ as Order) >> 1.00 and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) mockInventoryService.verify() then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Setup dependencies" 1 * mockInventoryService.removeFromStock(_) global 1 * mockShippingCostService.demand.calculate(_) >> 1.00 and: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) then: "total should be = price * quantity + shipping cost" total == 3.50 } }
@TestFor(OrderService) class OrderServiceSpec extends Specification{ InventoryService mockInventoryService InventoryService mockShippingCostService @Before void setup() { mockInventoryService = Mock() service.inventoryService = mockInventoryService mockShippingCalculatorService = Mock() service.shippingCostService = mockShippingCostService } @Test void "calculate order for one beer"() { given: "Create valid order" Item beer = new Item(name: 'Beer', price: 2.50) OrderItem orderItem = new OrderItem(item: beer, quantity:1) Order order = new Order() order.addItem(orderItem) when: "checkout the order" BigDecimal total = service.checkout(order) then: "total should be = price * quantity + shipping cost" total == 3.50 1 * mockInventoryService.removeFromStock(_) local 1 * mockShippingCostService.demand.calculate(_) >> 1.00 } }
1 * fooService.bar('baz') >> 'returnValue'
1 * fooService.bar('baz') >> 'returnValue'
1 * fooService.bar('baz') >> 'returnValue'
1 * fooService.bar('baz') >> 'returnValue'
1 * fooService.bar('baz') >> 'returnValue'
1 * fooService.bar('baz') >> 'returnValue'
1 * fooService.bar('baz') >> 'returnValue'
0 * fooService.bar('baz') >> 'returnValue'
( 1..3 ) * fooService.bar('baz') >> 'returnValue'
( 1.._ ) * fooService.bar('baz') >> 'returnValue'
( _..3) * fooService.bar('baz') >> 'returnValue'
_ * fooService.bar('baz') >> 'returnValue'
1 * mockTarget.methodName('mockParam') >> 'returnValue'
1 * _.methodName('mockParam') >> 'returnValue'
1 * mockTarget.methodName('mockParam') >> 'returnValue'
1 * mockTarget._('mockParam') >> 'returnValue'
1 * mockTarget./m.*e/('mockParam') >> 'returnValue'
1 * mockTarget.methodName("mockParam")
1 * mockTarget.methodName(!"mockParam")
1 * mockTarget.methodName( _ )
1 * mockTarget.methodName( _, _ )
1 * mockTarget.methodName( *_ )
1 * mockTarget.methodName( !null )
1 * mockTarget.methodName( _ as String )
1 * mockTarget.methodName( { it.size() > 3 } )
1 * mockTarget._(*_)
1 * mockTarget._
1 * _._
1 * _
_.._ * _._(*_) >> _
def mockFooService = Mock(FooService)
FooService mockFooService = Mock()
FooService mockFooService = Mock { 1 * getBar(_) }
2 * mockFooService.getBar() >> new Bar()
Mock() is swiss army knive of Interactions Could do mocking And Stubbingdef mockFooService = Stub(FooService)
FooService mockFooService = Stub()
FooService mockFooService = Stub { getBar(_) >> new Bar() }
mockFooService.getBar() >> new Bar()
Stub() Only care what return value if called Don't really care if it is calleddef mockFooService = Spy(FooService, constructorArgs: [2])
Implements an actual object Not just an interfase Pass constructor args2 * mockFooService.getBar()
Spies ALL method calls passthrough to the actual method2 * mockFooService.getBar() >> new Bar()
Can Stub return value
No longer passes through to actual object
Spies ALL method calls passthrough to the actual method@Ignore def "my test"() { ... }
@Ignore("Unstable test") def "my test"() { ... }
@Ignore class FooSpec extends Specification { ... }
@IgnoreRest def "only run me"() { ... }Method ONLY annotation
@IgnoreIf({System.getProperty('os.name').contains('X')}) def "osx no bueno"() { ... }
@IgnoreIf({System.getProperty('os.name').contains('X')}) class NoOSXSpec extends Specification() { ... }
@Require({System.getProperty('os.name').contains('X')}) def "only for OS X"() { ... }
@Stepwise class ExecuteInOrderSpec extends Specification() { def "I run first"() { ... } def "I run next"() { ... } }
class ListExtensions () { static avg(List list) { list.sum() / list.size() } } class MySpec extneds Specification() { @Use([ListExtensions]) def "averaging contents of a list"() { expect: [1, 2, 3].avg() == 2 } }
class ListExtensions () { static avg(List list) { list.sum() / list.size() } } class MySpec extneds Specification() { def "averaging contents of a list"() { expect: use(ListExtensions) { [1, 2, 3].avg() == 2 } } }
class MySpec extends Specification() { @ConfineMetaClassChanges([String]) def "mucking with string"() { when: String.metaClass.toUpperCase = { "FOO${delegate}" } then: "Baz".toUpperCase() == "FOOBaz" } def "more string stuff"() { when: String results = "foo".toUpperCase() then: results == "FOO" } }
class MySpec extends Specification() { @AutoCleanup BufferedReader xmlFileReader @AutoCleanup('dispose') mockDB def startup() { xmlFile = new File('foo.xml').newReader() mockDB = new MockDB() } }
class MySpec extends Specification() { @Shared BufferedReader xmlFileReader = new File(