我有一堂课试图检索设备的国家/地区:
context.getResources().getConfiguration().locale.getCountry();
Where context
属于类型:android.content.Context
所以在这里,context.getResources()
返回类型的对象android.content.res.Resources
.
在那个物体上,getConfiguration()
被调用,它返回一个类型的对象android.content.res.Configuration
.
关于这一点,我正在访问该领域locale
,其类型为java.util.Locale
.
在单元测试中,我试图模拟整个上下文:
Locale locale = new Locale(DEFAULT_LANGUAGE, DEFAULT_COUNTRY);
configuration = new Configuration();
configuration.setLocale(locale);
但是,我在这里收到错误,因为setLocale
实现为:
public void setLocale(Locale loc) {
throw new RuntimeException("Stub!");
}
或者,我尝试嘲笑整个Configuration
与 Mockito 一起上课:
mock(Configuration.class);
但是,我无法这样做,因为该类已声明final
.
那么,我如何模拟类型的对象android.content.res.Configuration
并给它一个区域设置?