Betamax is a great tool that records the output of a web service, and serializes the full request to a file. Once the request has been recorded, it can be played back in a test. This comes without any setup on your HTTP requests. This means that your system test no longer has a need to be connected to a working service, or has to be mocked based on a hand created mock.
Since the 2.3.8 release of GRAILs, there have been a few quirks to getting Betamax to work in your tests.
A few things need to be considered when using Betamax with Groovy Grails:
-
Conflicting dependencies with the build process and SnakeYML https://github.com/robfletcher/betamax/issues/153
-
The configuration file isn’t respected. Use the rule:
[groovy] @Rule Recorder recorder = new Recorder( tapeRoot: new File(BuildSettingsHolder.settings?.baseDir, ‘test/resources/tapes’), ignoreLocalhost: true, sslSupport: true) //The sort property isn’t set: set this up in the test setup TapePropertyUtils.metaClass.sort = { Set properties, List names -> new LinkedHashSet(properties.sort(true, new OrderedPropertyComparator(names))) } [/groovy]
1. Use Groovy to rewrite your RestClient [groovy] def meetupBase = new RESTClient(url) BetamaxRoutePlanner.configure(meetupBase.client) meetupBase.client.removeRequestInterceptorByClass(ContentEncoding.RequestInterceptor.class) meetupBase.client.removeResponseInterceptorByClass(ContentEncoding.ResponseInterceptor.class) meetupBase [/groovy]
-
Location of tape files in the project ~/ProjectFolder/test/resources/tapes
-
Note about the hostnames host reference. (All of the tapes are based on the mocked name, so be prepared to modify this if need be.)