Our normal way of writing unit tests is using mocks via Mockito. However, LocalBroadcastManager, for some unexplicable reason, is final - thus preventing Mockito from expanding it, which prevents us to mock/spy it...
--> How can I write unit tests for a class that contain LocalBroadcastManager?
I would for example like to check that when some conditions occur etc. certain broadcasts (containing specific extras) are sent out.
Use PowerMock:
Run your test class with PowerMock:
@RunWith(PowerMockRunner.class)
@PrepareForTest({LocalBroadcastManager.class})
Then where-ever in your test you want to mock the static method, do this:
PowerMockito.mockStatic(LocalBroadcastManager.class);
LocalBroadcastManager instance = mock(LocalBroadcastManager.class);
PowerMockito.when(LocalBroadcastManager.getInstance(context)).thenReturn(instance);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With