Android – use unit test notifications

I have a bunch of notification types, including the conditions I want to test automatically. My problem now is that I know I can't generate notifications and check whether the UI generated by the notification center is the same as expected

Therefore, I tried to decompose it into calling my broadcastreceiver and intercepting the point where I triggered the notification. So at that time, I can check whether the notification contains all the settings I expected. I have to hope that it will render as expected: -)

In my build.gradle, I added this block:

testOptions {
    unitTests.returnDefaultValues = true
}

This started my trouble. I created intent and called 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));
    }
}

This code crashes NullPointerException because I have this good line in broadcastreceiver:

switch(intent.getAction()) {

I can imagine what happened here. The simulation API will not create a real intent, and my data will disappear. How can I test my notification implementation? I think the pending intents I want to test next will not work

What can I do now?

resolvent:

My current solution is as follows:

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

However, this is only a solution. Later, I encountered the problem that the notification. Builder () class returned null in the builder mode, so it won't fix it at all

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>