How to create a new Bundle object?

风格不统一 提交于 2020-01-03 20:31:14

问题


I'm trying to use Firebase Analytics for an Android application, and in order to log events I've followed https://firebase.google.com/docs/analytics/android/events. That is, in order to send my event, I have to create a new Bundle object (which I create by using the default constructor) and I call the logEvent function of Firebase Analytics. While testing my development with a simple unit test, I realized that there's no content set in the bundle, which makes me wonder if any information is sent at all. Incidentally, it also breaks my test case.

Here's a simplified test case that shows my problem:

import android.os.Bundle;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;

public class SimpleTest {

    @Test
    public void test() {
        Bundle params = new Bundle();
        params.putString("eventType", "click");
        params.putLong("eventId",new Long(5542));
        params.putLong("quantity", new Long(5));
        params.putString("currency", "USD");

        assertEquals("Did not find eventType=click in bundle", "click", params.getString("eventType"));
    }
}

This test case fails with the following message:

junit.framework.ComparisonFailure: Did not find eventType=click in bundle
Expected :click
Actual :null

Would someone know where the problem is? That is, how do I create a Bundle object from zero and populate it correctly so that I can use it in a unit test like this?

Please bear with me on this one as I'm discovering the specifics of the Android environment as we speak.


回答1:


As pointed out by Tanis.7x in a comment to my original question, all Android framework classes need to be mocked as the android.jar used for running unit tests is empty as documented here.

Here's an updated version of my original simplified test case:

import android.os.Bundle;

import org.junit.Test;
import org.mockito.Mockito;

import static junit.framework.Assert.assertEquals;

public class SimpleTest {

    @Test
    public void test() {
        Bundle bundleMock = Mockito.mock(Bundle.class);
        Mockito.doReturn("click").when(bundleMock).getString("eventType");
        Mockito.doReturn(new Long(5542)).when(bundleMock).getLong("eventId");
        Mockito.doReturn(new Long(5)).when(bundleMock).getLong("quantity");
        Mockito.doReturn("USD").when(bundleMock).getString("currency");

        assertEquals("Did not find eventType=click in bundle", "click", bundleMock.getString("eventType"));
    }
}

The main difference is, that the variables I set earlier with simple getters are now set by using the appropriate functions of Mockito. The code is not as easy on the eyes, but it should allow me to obtain the wanted behaviour.




回答2:


Try using .equals() to compare strings as assertEquals() also uses the .equal() method for its working.



来源:https://stackoverflow.com/questions/38663513/how-to-create-a-new-bundle-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!