Skip to content

Vayras/wiremock-testcontainers-go

 
 

Repository files navigation

WireMock Module for Testcontainers Go

GoDoc GitHub release (latest by date) Slack GitHub contributors

This module allows provisioning the WireMock API mock server as a standalone container within your unit tests, based on the official WireMock Docker images (2.35.0-1 or above) or compatible custom images.

You can learn more about WireMock and Golang on this WireMock solutions page.

Supported features

The following features are now explicitly included in the module's API:

  • Passing API Mapping files
  • Passing Resource files
  • Sending HTTP requests to the mocked container

More features will be added over time.

Quick Start

See the Quick Start Guide. Just a teaser of how it feels at the real speed!

Quickstart demo GIF

Requirements

  • Golang version 1.17 or above, so all modern Golang projects should be compatible with it.
  • The module supports the official WireMock Docker images 2.35.0-1 or above.
  • Custom images are supported too as long as they follow the same CLI and API structure.

Usage

import (
  "context"
  . "github.com/wiremock/wiremock-testcontainers-go"
  "testing"
)

func TestWireMock(t *testing.T) {
	// Create Container
	ctx := context.Background()
	container, err := RunContainer(ctx,
		WithMappingFile("hello", "hello-world.json"),
	)
	if err != nil {
		t.Fatal(err)
	}

	// Clean up the container after the test is complete
	t.Cleanup(func() {
		if err := container.Terminate(ctx); err != nil {
			t.Fatalf("failed to terminate container: %s", err)
		}
	})

	// Send the HTTP GET request to the mocked API
	statusCode, out, err := SendHttpGet(container, "/hello", nil)
	if err != nil {
		t.Fatal(err, "Failed to get a response")
	}

	// Verify the response
	if statusCode != 200 {
		t.Fatalf("expected HTTP-200 but got %d", statusCode)
	}

	if string(out) != "Hello, world!" {
		t.Fatalf("expected 'Hello, world!' but got %v", string(out))
	}
}

License

The module is licensed under Apache License v.2

References

About

WireMock module for Testcontainers Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%