如何创建一个新的Bundle对象? [英] How to create a new Bundle object?

查看:143
本文介绍了如何创建一个新的Bundle对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Firebase Analytics用于Android应用程序,并且为了记录事件,我遵循了 https://firebase.google.com/docs/analytics/android/events .也就是说,为了发送事件,我必须创建一个新的Bundle对象(使用默认构造函数创建该对象),然后调用Firebase Analytics的logEvent函数.在用一个简单的单元测试来测试我的开发时,我意识到捆绑中没有设置任何内容,这使我想知道是否发送了任何信息.顺便说一句,这也破坏了我的测试用例.

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:未找到eventType =在捆绑包中单击
预期的:click
实际:null

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

有人知道问题出在哪里吗?也就是说,如何从零创建一个Bundle对象并正确填充它,以便可以在这样的单元测试中使用它?

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?

在我发现我们所说的Android环境细节时,请紧紧抓住这一点.

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

推荐答案

正如 Tanis.7x 在对我的原始问题的评论中指出的那样,所有Android框架类都必须作为android模拟如此处.

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

主要区别在于,我之前使用简单的getter设置的变量现在通过使用Mockito的适当函数进行设置.这段代码看起来不那么容易,但是它应该可以让我获得想要的行为.

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.

这篇关于如何创建一个新的Bundle对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆