# Mocks

> Verify requests received by WireMock mocks (stubs are defined in WireMock mapping files).

Source: https://axx.nimbusxr.us/references/steps/mock/

Verify requests received by WireMock mocks (stubs are defined in WireMock mapping files).

## `mock.service`

```gherkin
Given the mocked {word} service with the following properties:
  | ... | ... |
```

Register a WireMock server. The first mocked service registered in a scenario is the default one.

Properties: `url` (required; `${env:..}`/`${sys:..}` are expanded).

**Parameters:** `{word}` (one word, no spaces)

**Example:**

```gherkin
Given the mocked addresses service with the following properties:
```

## `mock.received`

```gherkin
Then the mocked {word} request to {word} named {word} was received by {mockedService}
```

Register a request pattern under a name (method + exact URL, including the query string) and verify WireMock received it at least once. Later steps refer to the pattern by name.

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Then the mocked GET request to /v1/postcodes/DE/10115 named postcode-check was received by addresses
```

## `mock.openapi.levels`

```gherkin
Given the OpenAPI validation levels for the mocked {mockedService} service are:
  | ... | ... |
```

Relax, for this scenario, the mocked service's OpenAPI contract: findings that would fail the checking mock step are reported at the level you set instead (`key | level` rows; WARN logs them, INFO and IGNORE drop them). A key also covers the keys below it: `validation.response.body` covers `validation.response.body.schema.required`. It applies to the calls this scenario's mock steps check; a stub that is off-contract on purpose is better relaxed in its own metadata (`openApiValidationLevels`), which applies wherever it answers. This is the dependency's contract: your own service's is relaxed with `the OpenAPI validation levels are:`.

**Parameters:** `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Given the OpenAPI validation levels for the mocked addresses service are:
```

## `mock.count.exactly`

```gherkin
Then the mocked request named {word}[[ on {mockedService}]] was received exactly {int} time(s)
```

Verify the named request pattern was received exactly the given number of times.

**Variants** (optional parts in `[[...]]` above):

- `the mocked request named {word} was received exactly {int} time(s)`
- `the mocked request named {word} on {mockedService} was received exactly {int} time(s)`

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario), `{int}` (a 32-bit integer)

**Example:**

```gherkin
Then the mocked request named postcode-check was received exactly 1 time
```

## `mock.count.atLeast`

```gherkin
Then the mocked request named {word}[[ on {mockedService}]] was received at least {int} time(s)
```

Verify the named request pattern was received at least the given number of times.

**Variants** (optional parts in `[[...]]` above):

- `the mocked request named {word} was received at least {int} time(s)`
- `the mocked request named {word} on {mockedService} was received at least {int} time(s)`

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario), `{int}` (a 32-bit integer)

**Example:**

```gherkin
Then the mocked request named postcode-check was received at least 1 time
```

## `mock.count.atMost`

```gherkin
Then the mocked request named {word}[[ on {mockedService}]] was received at most {int} time(s)
```

Verify the named request pattern was received at most the given number of times.

**Variants** (optional parts in `[[...]]` above):

- `the mocked request named {word} was received at most {int} time(s)`
- `the mocked request named {word} on {mockedService} was received at most {int} time(s)`

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario), `{int}` (a 32-bit integer)

**Example:**

```gherkin
Then the mocked request named postcode-check was received at most 1 time
```

## `mock.notReceived`

```gherkin
Then the mocked {word} request to {word} named {word}[[ on {mockedService}]] was not received
```

Register a request pattern under a name and verify WireMock received no matching request.

**Variants** (optional parts in `[[...]]` above):

- `the mocked {word} request to {word} named {word} was not received`
- `the mocked {word} request to {word} named {word} on {mockedService} was not received`

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Then the mocked GET request to /v1/postcodes/DE/12489 named skipped-check was not received
```

## `mock.header.is`

```gherkin
Then the header {word} for mocked request named {word}[[ on {mockedService}]] is {string}
```

Verify the named request was received with a header equal to the value. The constraint is added to the named pattern.

**Variants** (optional parts in `[[...]]` above):

- `the header {word} for mocked request named {word} is {string}`
- `the header {word} for mocked request named {word} on {mockedService} is {string}`

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario), `{string}` (text in single or double quotes; the quotes are removed)

**Example:**

```gherkin
Then the header X-Api-Key for mocked request named postcode-check is 'example-address-key'
```

## `mock.headers.are`

```gherkin
Then the headers for mocked request named {word} on {mockedService} are:
  | ... | ... |
```

Verify the named request was received with every header in the table (name | value).

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Then the headers for mocked request named postcode-check on addresses are:
```

## `mock.header.matches`

```gherkin
Then the header {word} for mocked request named {word} on {mockedService} matches {pattern}
```

Verify the named request was received with a header matching the regular expression (evaluated by WireMock, full match).

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario), `{pattern}` (A regular expression (Java syntax) without whitespace. It must match the whole value)

**Example:**

```gherkin
Then the header Accept for mocked request named postcode-check on addresses matches ^application/json$
```

## `mock.headers.match`

```gherkin
Then the headers for mocked request named {word} on {mockedService} match:
  | ... | ... |
```

Verify the named request was received with headers matching each regular expression in the table (name | pattern).

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Then the headers for mocked request named postcode-check on addresses match:
```

## `mock.header.missing`

```gherkin
Then the header {word} for mocked request named {word} on {mockedService} is missing
```

Verify the named request was received without the header.

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Then the header Authorization for mocked request named postcode-check on addresses is missing
```

## `mock.headers.missing`

```gherkin
Then the headers for mocked request named {word} on {mockedService} are missing:
  | ... | ... |
```

Verify the named request was received without any of the headers listed (one per row).

**Parameters:** `{word}` (one word, no spaces), `{mockedService}` (The name of a mocked service registered in the scenario)

**Example:**

```gherkin
Then the headers for mocked request named postcode-check on addresses are missing:
```
