---
title: "How to unit test C# methods that execute through Polly resilience pipelines?"  
description: "How to unit test C# methods that execute through Polly resilience pipelines?"  
author: "Ravi Vishwakarma"  
published: 2026-08-14  
updated: 2026-08-14  
canonical: https://answers.mindstick.com/qa/117060/how-to-unit-test-c-methods-that-execute-through-polly-resilience-pipelines  
category: "Polly"  
tags: ["csharp", "dotnet", "polly", "unit-testing", "xunit"]  
reading_time: 1 minute  

---

# How to unit test C# methods that execute through Polly resilience pipelines?

Testing code wrapped with resilience strategies requires verifying both success scenarios and failure/retry behaviors under simulated fault conditions.

## Unit Testing Pipelines

You can inject custom or dummy resilience pipelines into your domain logic during unit testing using dependency injection or mock builders.

### Code Example

```
[Fact]
public async Task Execute_OnTransientError_RetriesAndSucceeds()
{
    int attempts = 0;
    var pipeline = new ResiliencePipelineBuilder()
        .AddRetry(new RetryStrategyOptions
        {
            MaxRetryAttempts = 2,
            Delay = TimeSpan.Zero
        })
        .Build();

    await pipeline.ExecuteAsync(async ct =>
    {
        attempts++;
        if (attempts == 1)
        {
            throw new HttpRequestException("Transient error");
        }
        await Task.CompletedTask;
    });

    Assert.Equal(2, attempts);
}
```


---

Original Source: https://answers.mindstick.com/qa/117060/how-to-unit-test-c-methods-that-execute-through-polly-resilience-pipelines

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
