Java – how to create a new bundle object?

I'm trying to use firebase analytics for Android applications, and in order to log events, I follow https://firebase.google.com/docs/analytics/android/events. That is, in order to send events, I must create a new bundle object (by using the default constructor), Then I call the logEvent function of Firebase Analytics. When I test my development through simple unit tests, I realize that nothing is set up in the binding, which makes me wonder if I sent any information. By the way, it also destroys my test case.

This is 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"));
    }
}

The test case fails with the following message:

Does anyone know what the problem is? That is, how to create a bundle object from zero and fill it correctly so that it can be used in such a unit test?

When I find the details of Android environment in my speech, please keep in line with me

resolvent:

As tanis.7x pointed out in the comment on my original question, all Android framework classes need to be simulated because the android.jar used to run the unit test is empty, as shown in the document here

This is 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 used to set with a simple getter are now set by using the appropriate functions of mockito. This code doesn't seem so easy, but it should let me get the behavior I want

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
分享
二维码
< <上一篇
下一篇>>