Mocking azure blob storage in unit tests
Asked Answered
N

3

9

Is there anyway I can mock azure blob storage without running the storage emulator?It would be of great help if someone could shed some light on this

Negatron answered 4/9, 2013 at 10:21 Comment(0)
A
15

The Storage Library doesn't have an interface to use for mocking, so if you wanted to really mock this out I think you have two options:

  1. Create an interface yourself and hide the interaction with the storage library behind a class of your own. Then use your interface for the mocked tests. This is something I've done a lot of in the past, trying to abstract the use of the storage library away from the rest of the app. Of course, you can do this abstraction in your own code, or the storage library is out on GitHub. You could fork it and start adding interfaces to make the mocking easier. I think you'd have less work to just create an interface in your own code and a concrete implementation that did the necessary work to interact with the storage sub system for the things specific to your scenarios.

  2. Use a mocking framework that is capable of interception and can mock out types without interfaces. Something like TypeMock. There are others out there as well, both free and commercial.

Acerose answered 4/9, 2013 at 11:53 Comment(2)
Thanks for the answer, but the problem is have certain code that creates a blob container,and then upload a file to blob. Is there anyway I can write unit tests to these methods?Negatron
If you are wanting to test down to that detail against the actual storage library then you should look at TypeMock or Microsoft Fakes as user2746950 indicated in the other answer. These mocking frameworks should be able to let you intercept the calls to the actual storage libraries. Again, you could abstract this a way from your app using an interface and ensure the interface is called to do the work.Acerose
S
3

you can use Microsoft fakes, its simple and easy a free alternative for typemock

Stinkhorn answered 4/9, 2013 at 12:54 Comment(1)
Its not free, you need Visual Studio Enterprise to use this featureScleroderma
K
0

It's very easy to mock final things out in case you have Lombok on your classpath. Just extract whatever signatures you need from the azure blob client class to an interface, create a proxy class that delegates to the real azure client, and use the interface throughout our code. This way you can create mocks of the interface and have a little glue code that does not interfere with code coverage since lombok marks code as @Generated.

Here's how:

public interface AzureStorage {

  Mono<Boolean> exists();
    
  Mono<Void> delete();

  Mono<Response<BlockBlobItem>> uploadWithResponse(BlobParallelUploadOptions options);

  Mono<BlobDownloadAsyncResponse> downloadWithResponse(BlobRange range, DownloadRetryOptions options,
        BlobRequestConditions requestConditions, boolean getRangeContentMd5);
}

These are the methods we use in our project. And here's a class that implements the interface and delegates everything to the real azure blob client:

@lombok.Value
static class AzureStorageDelegate implements AzureStorage {

    @Delegate
    BlobAsyncClient client;

}

Now you can either mock the AzureStorageDelegate with Mockito or inject a mock AzureStorage wherever you see fit.

Kaenel answered 14/1, 2022 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.