May 2017
Intermediate to advanced
370 pages
8h 6m
English
Although Spock has a great selection of built-in features, it’s always useful to have extensibility. In this chapter, we look at various ways by which you can extend the default capabilities of your Spock specifications. We’ll look at composing assertions with Hamcrest matchers, providing reusable functionality with JUnit rules and Spock’s own extension mechanism.
We’ll refer back a couple of times in this chapter to a TimelineSpec specification class that tests the database queries and object marshalling around retrieving a user’s timeline.
We saw some variation on this earlier in the book, but let’s just revisit it quickly here:
classTimelineSpecextendsSpecification{@ShareddefdataSource=newJdbcDataSource(url:"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false")DBIdbi=newDBI(dataSource)@AutoCleanupHandlehandleUserStoreuserStoreMessageStoremessageStoreFollowingStorefollowingStore@SubjectUseruserUserfollowedUserUserotherUserdefsetup(){dbi.registerArgumentFactory(newTimeTypesArgumentFactory())dbi.registerMapper(newTimeTypesMapperFactory())// tag::fixtures[]handle=dbi.open()userStore=handle.attach(UserStore)userStore.createUserTable()messageStore=handle.attach(MessageStore)messageStore.createMessageTable()followingStore=handle.attach(FollowingStore)followingStore.createFollowingTable()user=userStore.insert("khan")followedUser=userStore.insert("kirk")otherUser
Read now
Unlock full access