A Behat Extension that makes use of Wiremock as a test double for API calls.
- Versions 1.x are compatible with behat 2.4+
- No compatibility for behat 3 (yet...)
- No compatibility for Windows
Via composer:
"require-dev": {
"creativesoftworks/behat-wiremock-context-extension": "1.*"
}
Your FeatureContext class must implement the CreativeSoftworks\BehatWiremockContextExtension\WiremockContextAware interface, here's an example:
class FeatureContext implements WiremockContextAware
{
/**
* @return \CreativeSoftworks\BehatWiremockContextExtension\WiremockContext
*/
public function getWiremockContext()
{
return $this->getSubcontext('WiremockContext');
}
/**
* @param \CreativeSoftworks\BehatWiremockContextExtension\WiremockContext $wiremockContext
*/
public function setWiremockContext(WiremockContext $wiremockContext)
{
$this->useContext('WiremockContext', $wiremockContext);
}
}
Configuration options, in your behat.yml:
your_profile_name:
extensions:
CreativeSoftworks\BehatWiremockContextExtension\Extension:
wiremock_base_url: 'http://localhost:8080'
wiremock_mappings_path: 'app/Resources/wiremock/mappings'
The extension will reset the wiremock mappings before each scenario execution, so all mappings under wiremock_mappings_path will get loaded with no guaranteed order. Frequently you would want to guarantee that certain mappings will become defaults in wiremock (usually successful responses instead of failed ones). In order for that to happen you can define an array of mappings to be loaded, relative to wiremock_mappings_path, like in this example:
default_mappings:
- { service: service-name, mapping: mapping-file.json }
- { service: service-name/and-subdirectory, mapping: another-mapping-file.json }
The WiremockContext provides a step definition to submit mappings to wiremock. Here's an example of a scenario using it:
Scenario:
Given the following services exist:
| service | mapping |
| greeting | helloWorld.json |
When I go to the "Hello World" page
Then I should see "Hello World!"
The first step in the scenario POSTs the mapping information in the file %wiremock_mappings_path%/greeting/helloWorld.json to the Wiremock server. Check this for more information about wiremock stubbing using JSON mapping files.