Skip to content
Martijn Bodeman edited this page Jun 7, 2023 · 8 revisions

IbanGenerator

Use the IbanGenerator class to generate random IBAN's. The IBAN's generated will adhere to the country specific rules and have valid check digits. While the IBAN produced will pass validation, it may not be an actual valid bank account and - for obvious reasons - should ONLY be used for testing purposes.

If you are instead looking for a way to 'build' an IBAN from a BBAN, have a look here.

There are a few ways to generate an IBAN:

Via ctor with default registry

public class TestClass
{
    [Fact]
    public void MyTest()
    {
        var generator = new IbanGenerator();
        Iban iban = generator.Generate("NL");

        // Act
        // use the generated IBAN...
    }
}

Via ctor with an IIbanRegistry

public class TestClass
{
    [Fact]
    public void MyTest()
    {
        var generator = new IbanGenerator(new IbanRegistry { /* custom providers */ });
        Iban iban = generator.Generate("NL");
    }
}

Via dependency injection

At runtime, when using one of the DI integration packages, you can also request an instance of IIbanGenerator.

public class MyClass
{
    public MyClass(IIbanGenerator generator)
    {
        _generator = generator;
    }

    public void Hello()
    {
        Iban iban = _generator.Generate("NL")
    }
}

More information

Check out this fiddle for a working example.