If you have the Golang development environment ready to go and do not want a step by step guide,
you can just clone the project’s repository in
wiremock/wiremock-testcontainers-go
,
go to the examples/quickstart
directory
and run go build
and then go test
in the root or try the examples.
Any pull requests will be welcome ;-)
- Go 1.19 or above
- Docker-API compatible container runtime (more info)
Create the go.mod
file with the following content:
module wiremock.org/testcontainers-go-quickstart
go 1.19
require (
github.com/pkg/errors v0.9.1
github.com/wiremock/wiremock-testcontainers-go v1.0.0-alpha-4
)
Then, run go mod install
to install the dependencies and prepare the environment
Create a quickstart_test.go
file with the package name.
Add dependencies we will need for this demo, and also create the test stub:
package testcontainers_wiremock_quickstart
import (
"context"
. "github.com/wiremock/wiremock-testcontainers-go"
"testing"
)
func TestWireMock(t *testing.T) {
// Our future work will be here
}
For our demo, we will need to expose a test WireMock Mapping.
Create the hello-world.json
file with the following content:
{
"request": {
"method": "GET",
"url": "/hello"
},
"response": {
"status": 200,
"body": "Hello, world!"
}
}
In func TestWireMock(t *testing.T)
, add the following code:
// 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)
}
})
Now, we will need to send an HTTP request to our test API. To do so, we will use a build-in method:
func TestWireMock(t *testing.T) {
// ... Previous initialization code
// Send a simple 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")
}
// ... Validation will be here
}
In the code above, we used the SendHttpGet
method to send a HTTP GET request. The library also offers methods to send requests with other HTTP methods, i.e. SendHttpPost
, SendHttpDelete
,SendHttpPatch
, SendHttpPut
.
Now, add the verification logic that will check correctness of the WireMock response:
func TestWireMock(t *testing.T) {
// ... Previous initialization code
// ... Previous HTTP request send code
// 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))
}
}
We are finally ready to run the test! Do the following:
go test
If everything goes right, you will see the following console output:
See the documentation root for the references to more features, examples and demos.