WireMock for API Mocking - NextensArelB/SwaggerGenerationTool GitHub Wiki
WIP
[[TOC]]
WireMock Opensource API mock tool.
Flexible API Mocking for Testing and Development. Enables you to test your API isolated by mocking the responses. This way you can create reliable test scenarios. Development time with this tool will be reduced as it can be used to replicate unreliable third party services. Additionally, it gives the option to test unhappy flows with error responses.
Pro:
- Small footprint
- SaaS, nothing to install
- Can run in Docker
Con:
- Is a Java product (not native for our company)
You can find a setup guide for WireMock here
You can run WireMock standalone by executing this command in the command window, in the folder where the jar is located:
java -jar wiremock-standalone-3.0.4.jar --port [Port]
##Stubbing (Creating mapping file) example In the mapping sub-folder you can create the stubs as explained Here In this example have stubbed call to Exact: GetCompanies. The response is JSON which means the stub file looks like this:
{
"request": {
"method": "GET",
"url": "/api/v2/provider/Exact/companies"
},
"response": {
"status": 200,
"jsonBody": {
"d": {
"results": [
{
"__metadata": {
"uri": "https://start.exactonline.nl/api/v1/974601/system/Divisions(974601)",
"type": "Exact.Web.Api.System.Division"
},
"Code": 974601,
"ChamberOfCommerceNumber": "1234567890",
"Description": "BV DEMO hele boekjaren _MOCK_",
"Hid": "1",
"VATNumber": "NL797259442B93"
}, [...]
In this example (Seen Above) we mock the response message for Exact using the GetCompanies call.
##Include a WireMocke API in the Azure Pipeline [WIP] In order to run your Api with Wiremock in a release pipeline you need to make some configuration changes:
- Folder structure in your repository
- The release pipeline YAML
- Powershell script for running the API test against the mocked API
###Folder stucture in your repository The following structure can be generated in your repo so the API can run in the pipleline:
#[Your repo root folder] [application]-api-runmocktests.yml - The pipeline definition file - Description of the contents below
#[Your repo root folder]/test/mock/ runmocktests.ps1 - Description of the contents below
#[Your repo root folder]/test/mock/karate [This folder contains the Karate sub-folder: 'test', having the feature files, and the Karate.jar in the root of this folder.]
#[Your repo root folder]/test/mock/WireMock [Having the sub-folder 'mappings' containg reposponse (mock) json files and in the root the wiremock-standalone.jar] Start_wiremock_standalone.bat - Description of the contents below
###Contents sample for [application]-api-runmocktests.yml
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- main
pool:
vmImage: 'windows-latest'
steps:
- task: JavaToolInstaller@0
displayName: 'Set Java JDK to v11'
inputs:
versionSpec: '11'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
- task: NuGetAuthenticate@1
- task: PowerShell@2
displayName: 'Run runmocktests.ps1'
inputs:
filePath: 'test/mock/runmocktests.ps1'
# Publish the Karate JUnit XML test results that were saved to:
# - test\mock\karate\target\karate-reports\tests.exact.getCompanies.xml
# - test\mock\karate\target\karate-reports\tests.exact.getPeriods.xml
- task: PublishTestResults@2
displayName: 'Publish Karate JUnit XML Test Results'
enabled: true
inputs:
failTaskOnFailedTests: true
failTaskOnMissingResultsFile: true
testResultsFormat: 'JUnit'
testResultsFiles: '**/tests.exact.*.xml'
###Contents sample for runmocktests.ps1
# Get the full path of the script file
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# Resolve any relative paths in case script was invoked from a different location
$fullScriptPath = Resolve-Path -LiteralPath $scriptPath
try {
# Change current working directory to solution location
Push-Location -LiteralPath $fullScriptPath
Push-Location ..\..\src
# Build solution in mock
$solutionPath = $PWD.Path + "\DataHub.FinancialData.sln"
Write-Host "`nBuilding solution from $solutionPath"
dotnet build $solutionPath /p:EnvironmentName=Mock /property:WarningLevel=0
# run solution
$executablePath = $PWD.Path + "\DataHub.FinancialData.WebApi\bin\Debug\net6.0"
Write-Host "`nStarting Financial Data API from $executablePath"
$fdApiProcess = Start-Process -FilePath $executablePath
# run wiremock
Push-Location $fullScriptPath
Push-Location "WireMock"
Write-Host "`nStarting WireMock from $fullScriptPath\WireMock"
$wireMockProcess = Start-Process java "-jar wiremock-standalone-3.0.4.jar --port 55186 --verbose"
# run karatetests
Push-Location $fullScriptPath
Push-Location "karate"
Write-Host "`nStarting the following Karate tests from $fullScriptPath\karate:"
Get-ChildItem -Path $fullScriptPath\karate\tests\exact\*.feature | ForEach-Object { Write-Host ("- {0}" -f $_.Name) }
Write-Host ""
Start-Process -FilePath java -ArgumentList "-jar", "karate.jar", "-Dkarate.options=--exit-after-features --format junit:xml -clean ", "$fullScriptPath\karate\tests\exact\*.feature" -NoNewWindow -Wait
<#
Next - publish test results that were saved to:
- test\mock\karate\target\karate-reports\tests.exact.getCompanies.xml
- test\mock\karate\target\karate-reports\tests.exact.getPeriods.xml
#>
}
finally {
Pop-Location
# tear everything down
foreach ($p in $fdApiProcess) {
Stop-Process -Id $p.Id
}
foreach ($p in $wireMockProcess) {
Stop-Process -Id $p.Id
}
}
###Contents sample for Start_wiremock_standalone.bat
java -jar wiremock-standalone-3.0.4.jar --port 55186 --verbose
##Create the Pipeline Now in Azure devops if these files are added to the repo (and main branch) you can add the pipeline and follow the instructions to pick up the yaml.