# API_MockFactory.MockBuilder

**Class** · Group: `Web Services`

Learn it: [Fast Start - Outbound APIs](/fast-start-outbound-apis) and [Web Services guide](/web-services-guide)

<div class="apex-member apex-class">

```apex
global class API_MockFactory.MockBuilder
```

Fluent builder for constructing and registering mock responses.

**Example**

```apex
API_MockFactory.forService('API_SendEmail')
    .body('{"messageId":"msg-123"}')
    .statusCode(200)
    .register();
```

</div>

---

## Methods

| Method | Description |
|--------|-------------|
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [body](#body)([String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) body) | Sets the response body. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [fromResponse](#fromresponse)([HttpResponse](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm) response) | Populates the builder from an existing HttpResponse. |
| global void [register](#register)() | Registers the mock response in the factory. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [status](#status)([String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) status) | Sets the HTTP status text. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [statusCode](#statuscode)([Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm) statusCode) | Sets the HTTP status code. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [withFailureRate](#withfailurerate)([Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm) failureRate) | Sets the failure rate percentage for fault injection. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [withHeader](#withheader)([String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) name, [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) value) | Adds a single response header. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [withHeaders](#withheaders)([Map](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_map.htm)<[String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm), [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm)> headers) | Adds multiple response headers. |
| global [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) [withTransportFailure](#withtransportfailure)() | Makes the registered mock simulate a transport failure: the callout throws a CalloutException before any response exists, so the caller sees no status code at all. |

### body

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder body(String body)
```

Sets the response body.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `body` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The response body (supports {{request.field}} interpolation) |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### fromResponse

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder fromResponse(HttpResponse response)
```

Populates the builder from an existing HttpResponse.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `response` | [HttpResponse](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm) | The HttpResponse to extract body, status code, and headers from |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### register

<div class="apex-member">

```apex
global void register()
```

Registers the mock response in the factory.

</div>

### status

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder status(String status)
```

Sets the HTTP status text.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `status` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The HTTP status text (e.g., "OK", "Internal Server Error") |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### statusCode

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder statusCode(Integer statusCode)
```

Sets the HTTP status code.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `statusCode` | [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm) | The HTTP status code |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### withFailureRate

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder withFailureRate(Integer failureRate)
```

Sets the failure rate percentage for fault injection.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `failureRate` | [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm) | Percentage of requests to fail (0-100) |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### withHeader

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder withHeader(String name, String value)
```

Adds a single response header.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `name` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The header name |
| `value` | [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm) | The header value |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### withHeaders

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder withHeaders(Map<String, String> headers)
```

Adds multiple response headers.

**Parameters**

| Parameter | Type | Description |
|-----------|------|-------------|
| `headers` | [Map](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_map.htm) | Map of header key-value pairs |

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining

</div>

### withTransportFailure

<div class="apex-member">

```apex
global API_MockFactory.MockBuilder withTransportFailure()
```

Makes the registered mock simulate a transport failure: the callout throws
a CalloutException before any response exists, so the caller sees no status code at
all. Use it to prove code that must treat a statusless failure differently from a
failed status — for example, chain-level retryOn keeps statusless failures retryable.
Body, status code, and headers on the same registration are ignored: a transport
failure never produces a response.

**Returns** [API_MockFactory.MockBuilder](/reference/apex/api-mockfactory-mockbuilder) — This builder for chaining.

**Example**

```apex
API_MockFactory.forService('API_SendEmail').withTransportFailure().register();
```

</div>
