模拟 android.content.res.Configuration 类型的对象并为其分配区域设置 [英] Mock object of type android.content.res.Configuration and assign it a locale

查看:56
本文介绍了模拟 android.content.res.Configuration 类型的对象并为其分配区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个课程,我试图检索设备所在的国家/地区:

I have a class where I am trying to retrieve the country of the device:

context.getResources().getConfiguration().locale.getCountry();

其中 context 的类型为:android.content.Context

所以在这里,context.getResources() 返回一个 android.content.res.Resources 类型的对象.

So here, context.getResources() returns an object of type android.content.res.Resources.

在那个对象上,getConfiguration() 被调用,它返回一个 android.content.res.Configuration 类型的对象.

On that object, getConfiguration() is called, which returns an object of type android.content.res.Configuration.

在那,我正在访问字段 locale,它的类型是 java.util.Locale.

On that, I am accessing the field locale, which is of type java.util.Locale.

在单元测试中,我试图模拟整个上下文:

In a unit test, I am trying to mock this whole context:

Locale locale = new Locale(DEFAULT_LANGUAGE, DEFAULT_COUNTRY);
configuration = new Configuration();
configuration.setLocale(locale);

然而,在这里我收到一个错误,因为 setLocale 被实现为:

Here, however, I am getting an error because setLocale is implemented as:

public void setLocale(Locale loc) {
    throw new RuntimeException("Stub!");
}

或者,我尝试使用 Mockito 模拟整个 Configuration 类:

Alternatively, I tried mocking the whole Configuration class with Mockito:

mock(Configuration.class);

但是,我无法这样做,因为该类声明为 final.

However, I am not able to do that because the class is declared final.

那么,我如何模拟 android.content.res.Configuration 类型的对象并为其指定语言环境?

So then, how can I mock an object of type android.content.res.Configuration and give it a locale?

推荐答案

这就是如何使用 Mockito 我也在那里重新发布我的答案.

This is how use Mockito I re-post my answer there too.

示例

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Locale;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class Test2 {

    @Mock
    Context mMockContext;

    @Test
    public void getLocal() {
        Resources resources = Mockito.mock(Resources.class);
        when(mMockContext.getResources()).thenReturn(resources);
        Configuration configuration = Mockito.mock(Configuration.class);
        when(mMockContext.getResources().getConfiguration()).thenReturn(configuration);
        LocaleList localeList = Mockito.mock(LocaleList.class);
        when(mMockContext.getResources().getConfiguration().getLocales()).thenReturn(localeList);
        when(mMockContext.getResources().getConfiguration().getLocales().get(0)).thenReturn(Locale.CANADA);
        System.out.println(mMockContext.getResources().getConfiguration().getLocales().get(0));
    }
}

系统输出

en_CA

Process finished with exit code 0

Mockito 文档

来自 java.lang.NoSuchMethodError: android.content.res.Configuration.setLocale(Ljava/util/Locale;)V

这篇关于模拟 android.content.res.Configuration 类型的对象并为其分配区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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