Sharing Transactions Between Mock Services and Wiremock

You can add stubs from external mocking tools to use those definitions as Mock Services in BlazeMeter.

The following external tools are supported:

You can integrate Transactions between Mock Services and Wiremock in the following ways:

Prerequisites for In-Code Sharing Between Mock Services and Wiremock

To interact with the Transaction repository directly from your Wiremock code, ensure that you meet the following prerequisites.

These prerequisites are not required if you are simply uploading JSON files exported from Wiremock.

Wiremock Prerequisites

The Wiremock extension library is required to connect with BlazeMeter Mock Services:

Maven:

<repositories>
  <!-- your other repositories -->
  <repository>
    <id>blazemeter</id>
    <name>blazemeter</name>
    <url>http://blazemeter.jfrog.io/artifactory/blazemeter-public/</url>
  </repository>
</repositories>
<dependency>
  <groupId>com.perforce.blazemeter-wiremock-extension</groupId>
  <artifactId>blazemeter-wiremock-extension</artifactId>
  <version>1.0.3</version>
</dependency>

Gradle:

repositories {
  maven {
    url "http://blazemeter.jfrog.io/artifactory/blazemeter-public/"
  }
}
dependencies {
  compile 'com.perforce.blazemeter-wiremock-extension:blazemeter-wiremock-extension:1.0.3'
}

Only version 1.0.3 of BlazeMeter-WireMock extension supports ability to upload transactions from code to BlazeMeter Asset Catalog and to download transactions from BlazeMeter Asset Catalog to code.

Exporting Transactions to a JSON file

Export virtual endpoint definitions from your code into a JSON transaction file, which you can then import into the Transaction Repository through the Mock Services UI. Using this method, developers using mocking tools can share the artifacts they created in their code with other team members who can load them into the Mock Services UI, augment them as needed, and run them as Mock Services hosting the content imported from the code.

Wiremock Code Samples:

public class WiremockExportExampleTest {
	@Rule
	public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8081));
	WiremockTxnRepoStore store = new WiremockTxnRepoStore.StoreBuilder()
			.withTestInstance(this).withWireMockServerOrRule(wireMockRule)
			.exportAsDslFile("/tmp/dsl")
			.build();
	@Test
	public void wiremockExportExampleTest() {
		stubFor(get(urlEqualTo("/cities"))
				.willReturn(aResponse()
				.withStatus(200).withBody("...cities...")));
		
		/* rest of the test */			
	}
}

The vanilla Wiremock library lets you export a JSON DSL file using the WireMock.saveAllMappings(); method. Any file exported using this approach could be imported to Mock Services.

After you export the definitions into JSON, you can add them as Transactions from a file to the Transaction Repository.

Upload Transactions to Transaction Repository from Code

You can directly upload in-code definitions of virtual endpoints to the Transaction Repository. Once the test with these definitions runs, they are converted from their in-code form into Transaction format and uploaded to the repository.

This is another way that developers using Wiremock can share the artifacts they created in code with other team members, who can use and augment them in the UI as Mock Services.

For more information about the apiKey and apiSecret variables, see BlazeMeter API Keys.

Wiremock Code Samples:

@TransactionCloudRepository(
		workspaces = {"Default Workspace"},
		apiKey = "...",
		apiSecret = "...",
		uri = "https://mock.blazemeter.com"
)
public class WiremockUploadExampleTest {
	@Rule
	public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8081));
	private WiremockTxnRepoStore store = new WiremockTxnRepoStore.StoreBuilder()
			.withTestInstance(this).withWireMockServerOrRule(wireMockRule)
			.uploadDslToTransactionRepository("Default Workspace", "Default Service")
			.build();
	@Test
	public void wiremockUploadExampleTest() {
		stubFor(get(urlEqualTo("/cities"))
				.willReturn(aResponse()
				.withStatus(200).withBody("...cities...")));
										
		/* rest of the test */
	}
}

When you use this method, the Transactions included in the test are automatically loaded into the Transaction Repository.

Reference Transaction Repository from Code

For Transactions that already exist in the Repository, you can reference them directly from the code in your mocking tools. This lets developers use Transaction definitions that other team members created in their code.

For more information about the apiKey and apiSecret variables, see BlazeMeter API Keys.

Wiremock Code Samples:

@TransactionCloudRepository(
		workspaces = {"Default Workspace"},
		apiKey = "...",
		apiSecret = "...",
		uri = "https://mock.blazemeter.com"
)
public class WiremockRepoExampleTest {
	@Rule
	public WireMockRule wireMockRule = new WireMockRule(
			wireMockConfig().port(8081));
	private WiremockTxnRepoStore store = new WiremockTxnRepoStore.StoreBuilder()
			.withTestInstance(this).withWireMockServerOrRule(wireMockRule)
			.build();
	@Test
	public void wiremockRepoExampleTest() {
		// virtualization based on Transactions from Repository
		store.useTransaction("Example transaction", "Default Service");
		/* rest of the test */
	}
}