Testing notifications with unit tests

I have a group of notification types with conditions that I want to check automatically. My problem is that I know that it is not possible to create a notification and check if the generated user interface looks like in the notification center.

Because of this, I tried to break it before my call BroadcastReceiverand intercept the point at which I start the notification. So at this moment I can check if the notification contains all the settings that I expect. I should hope that this will be done as expected :-)

In mine, build.gradleI added this block:

testOptions {
    unitTests.returnDefaultValues = true
}

This is where my problem begins. I create Intentand call the receiver:

@RunWith(MockitoJUnitRunner.class)
public class NotificationTest {
    @Mock
    Context mMockContext;

    @Test
    public void firstTest() {
        NotificationManager manager = new NotificationManager();
        manager.onReceive(mMockContext, new Intent(NotificationManager.MY_ACTION));
    }
}

NullPointerException, BroadcastReceiver:

switch(intent.getAction()) {

, . API Intent, . ? , PendingIntent, , .

?

+4
1

:

Intent action = spy(new Intent(NotificationManager.MY_ACTION));
doReturn(NotificationManager.MY_ACTION).when(action).getAction();

. , Notification.Builder() , .

0

Source: https://habr.com/ru/post/1608424/


All Articles