class: middle, center # Spock "The enterprise ready specification framework" https://github.com/spockframework/spock/ --- class: middle, left ## Spock - JUnit ``` | Spock | Junit | |---------------------|------------------------------------| | Specification | Test Class | | setup() | @Before | | cleanup() | @After | | setupSpec() | @BeforeClass | | cleanupSpec() | @AfterClass | | Feature | Test | | Feature method | Test method | | Data-driven feature | Theory | | Condition | Assertion | | Exception condition | @Test(expected=…) | | Interaction | Mock expectation (e.g. in Mockito) | ``` --- class: middle, left ## Specification A specification is represented as a Groovy class that extends from `spock.lang.Specification` ```groovy class MyFirstSpecification extends Specification { // fields // fixture methods // feature methods // helper methods } ``` --- class: middle, left ## Fixtures ```groovy def setup() {} // run before every feature method def cleanup() {} // run after every feature method def setupSpec() {} // run before the first feature method def cleanupSpec() {} // run after the last feature method ``` --- class: middle, left ## Feature Methods * Set up the feature’s fixture * Provide a stimulus to the system under specification * Describe the response expected from the system * Clean up the feature’s fixture ```groovy def "pushing an element on the stack"() { // blocks go here } ``` --- class: middle, left ## Blocks There are six kinds of blocks: `setup`, `when`, `then`, `expect`, `cleanup`, and `where` blocks.  --- ### Feedback ```groovy setup: def stack = new Stack() def elem = "push me" when: stack.push(elem) then: !stack.empty stack.size() == 1 stack.peek() == elem ``` What kind of feedback does Spock provide if a condition is violated? (`stack.size() == 2`) ```groovy Condition not satisfied: stack.size() == 2 | | | | 1 false [push me] ``` --- class: middle, left ## Mocking/Stubbing JUnit ```java @RunWith(MockitoJUnitRunner.class) ... private Subscriber subscriber = mock(Subscriber.class); ... when(subscriber.someMethod(TEST)).thenReturn(result); verify(subscriber).otherMethod(isA(Some.class)); ``` Spock ```groovy def subscriber = Mock(Subscriber) ... subscriber.someMethod(TEST) >> result 1 * subscriber.otherMethod ``` --- class: middle, left ## Parameterization ```groovy class ImageValidatorSpec extends Specification { @Unroll def "validate extension of #fileToValidate"() { when: "validator checks filename" def isValid = validate fileToValidate then: "return appropriate result" isValid == expectedResult where: "input files are" fileToValidate || expectedResult 'some.jpeg' || true 'some.jpg' || true 'some.tiff' || false 'some.bmp' || true 'some.png' || false } } ``` --- class: middle, left ## Specifications as Documentation ```groovy class AlbumServiceSpec extends Specification { AlbumServiceImp service AlbumMapper mapper = Stub(AlbumMapper) void setup() { service = new AlbumServiceImp(mapper) } void "It should return dto list when the find is correct"() { given: "stubbing service" Integer userId = 3 List
dataSearch = [] mapper.findAllByUserId(_ as Long) >> dataSearch when: "store service response" def findInformation = service.findAllByUserId(userId) then: "assertions" findInformation instanceof List
} } ```