B.8. Spock dbUnit extension

Chapter 7 shows how to use the Groovy SQL package to preload your database with test data needed for the Spock test. If you like this approach, you can take it a step further with the Spock dbUnit extension (https://github.com/janbols/spock-dbunit).

The extension is based on the well-known DbUnit library (http://dbunit.sourceforge.net/), initially developed for JUnit, that can initialize a database to a known state, reading data from XML files. The Spock dbUnit extension allows you to use Groovy code instead of XML files, as shown in the following listing.

Listing B.7. Fail all tests after three tries

Data on the DB is saved via a data source, which can be created by hand, or—even better—injected by Spring test, as shown in the next listing.

Listing B.8. Using dbUnit Spock with Spring

At the time of writing, this Spock extension is only in JCenter (https://bintray.com/bintray/jcenter), not Maven Central, so you must either build it yourself or configure JCenter in your pom.xml.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.186