-
Notifications
You must be signed in to change notification settings - Fork 2k
Mocking Service Clients
Connie Yau edited this page Jan 23, 2020
·
2 revisions
Customers using the Azure SDK client libraries can mock service clients using Mockito with org.mockito.plugins.MockMaker
. More information can be found at Mock the unmockable: opt-in mocking of final classes/methods.
- Create a file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
- In the file, put a single line.
mock-maker-inline
Consider an application that uses Azure Event Hubs to fetch telemetry and the end user wants to test that their TelemetryEvents
class properly transforms EventData
to TelemetryEvent
.
public class TelemetryEvents {
private final EventHubConsumerAsyncClient consumerClient;
public TelemetryEvents (EventHubConsumerAsyncClient consumerClient) {
this.consumerClient = consumerClient;
}
public Flux<TelemetryEvent> getEvents() {
return consumerClient.receiveFromPartition("1", EventPosition.latest())
.map(event -> new TelemtetryEvent(event));
}
}
import reactor.test.publisher.TestPublisher;
import reactor.test.StepVerifier;
import static com.azure.messaging.eventhubs.*;
import static org.mockito.Mockito.*;
public class TelemetryEventsTest {
@Test
public void canGetEvents() {
// Arrange
// After following the instructions in "Steps" section
EventHubConsumerAsyncClient consumerClient = mock(EventHubConsumerAsyncClient.class);
TestPublisher<EventData> eventsPublisher = TestPublisher.createCold();
eventsPublisher.emit(new EventData("Foo"), new EventData("Bar"));
when(consumerClient.receiveFromPartition(eq("1"), eq(EventPosition.latest())).thenReturn(eventsPublisher.flux());
TelemetryEvents telemetryEvents = new TelemetryEvents(consumerClient);
// Act
StepVerifier.create(telemetryEvents.getEvents())
.assertNext(event -> isMatchingTelemetry(event))
.assertNext(event -> isMatchingTelemetry(event))
.verifyComplete();
}
}
- Frequently Asked Questions
- Azure Identity Examples
- Configuration
- Performance Tuning
- Android Support
- Unit Testing
- Test Proxy Migration
- Azure Json Migration
- New Checkstyle and Spotbugs pattern migration
- Protocol Methods
- TypeSpec-Java Quickstart
- Getting Started Guidance
- Adding a Module
- Building
- Writing Performance Tests
- Working with AutoRest
- Deprecation
- BOM guidelines
- Release process
- Access helpers