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
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:
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.
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.
you can use Microsoft fakes, its simple and easy a free alternative for typemock
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.
© 2022 - 2024 — McMap. All rights reserved.