I have the class handleFirebaseMessages
. It contains the function onMessageReceived()
. This function is in charge of receiving data and creating notifications. onMessageReceived()
receives data in a RemoteMessages
object. I am trying to write a test for this function. But I've not got it completely.
HandleFirebaseMessages
public class HandleFirebaseMessages extends FirebaseMessagingService {
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
final Map<String, String> data = remoteMessage.getData();
//create notifications using NotificationCompat.Builder
}
}
I've been able to write a part of the test code. How do I complete this?
@Test
public void testHandleFirebaseMessages() throws Exception {
Map<String,String> data=new HashMap<String,String>();
data.put("id","4422");
data.put("type", "1");
data.put("imageUrl" , "https://image.freepik.com/free-vector/android-boot-logo_634639.jpg");
data.put("smallTitle" , "DoJMA v2");
data.put("smallSubTitle", "Update now from Google Play Store");
data.put("ticker" , "New update for DoJMA");
data.put("contentInfo" , "");
data.put("link" , "https://photo2.tinhte.vn/data/avatars/l/1885/1885712.jpg?1402763583");
data.put("className" , "HomeActivity");
data.put("page" , "2");
data.put("bigTitle" , "DoJMA Android app version 2 released!");
data.put("bigSubTitle" , "Hi folks! New DoJMA update is here! Major redesigning and improvements! This app was made by the Mobile App Club.They work really hard man...and get good products");
data.put("bigSummaryText" , "Update now");
RemoteMessage message = new RemoteMessage.Builder("1")
.setMessageId("1")
.setData(data)
.build();
(new HandleFirebaseMessages()).onMessageReceived(message);
//WHat now?
}
You test this by wrapping the Notifications
API to make it testable. First you will need a way of passing in mocked dependencies to the constructor of your wrapper. You can write something like this:
class NotificationBuilderProvider {
//TODO: implement Provider<NotificationCompat.Builder> if you are using JSR-330 annotations
private Context context;
NotificationBuilderProvider(Context context) {
this.context = context;
}
@Override
public NotificationCompat.Builder get() {
return new NotificationCompat.Builder(context);
}
}
Now you can write a wrapper like this:
class NotificationsWrapper {
private final NotificationBuilderProvider notificationBuilderProvider;
private final NotificationManager notificationManager;
NotficiationsWrapper(NotificationBuilderProvider notificationBuilderProvider, NotificationManager notificationManager) {
this.notificationBuilderProvider = notificationBuilderProvider;
this.notificationManager = notificationManager;
}
public void postNotification(Map<String, String> data) {
Notification notification = notificationBuilderProvider.get()
.setContentTitle(data.get("Title"))
//etc
.build();
notificationManager.notify(ID, notification);
}
}
Now you can write a unit test for your NotificationWrapper
which is easier to testing than testing the heavyweight service:
//mocks
NotificationBuilderProvider mockNotificationBuilderProvider;
NotificationBuilder mockNotificationBuilder;
NotificationManager mockNotificationManager;
//system under test
NotificationWrapper notificationWrapper;
@Before
public void setUp() {
mockNotificationBuilderProvider = Mockito.mock(NotificationBuilderProvider.class);
mockNotificationBuilder = Mockito.mock(NotificationCompat.Builder.class, RETURNS_SELF);
mockNotifyManager = Mockito.mock(NotificationManager.class);
}
@Test
public void givenTitleInData_whenPost_thenNotificationHasContentTitle() {
//arrange
Map<String, String> data = new HashMap<>();
data.put("Title", "MyTitle");
//act
notificationsWrapper.postNotification(data);
//assert
verify(notificationBuilder).setContentTitle(eq("Title"));
}
If this all seems a little complicated, it's better to start with writing unit tests for simple classes until you are comfortable with the concepts of unit testing and mocking. Then you can move up to testing more complex classes. The Mockito documentation is a great place to start to learn about mocking and unit testing. Good luck!
Now in the onMessageReceived(message), create a custom notification.
Try this link Custom notificatoin
Hope this helps.