- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 AndroidX 中, InstrumentationRegistry 现在已被弃用。 documentation状态
This method is deprecated. In most scenarios, getApplicationContext() should be used instead of the instrumentation test context. If you do need access to the test context for to access its resources, it is recommended to use getResourcesForApplication(String) instead.
PackageManager
实例的示例。在测试中调用
getResourcesForApplication
以及应将哪个包名称提供给其字符串参数。
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.IOException;
import java.io.InputStream;
import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class)
public class MyTest {
@Test
public void processImage() {
// load image from test assets
AssetManager am = InstrumentationRegistry.getContext().getAssets();
InputStream is = null;
Bitmap image = null;
try {
is = am.open("image.jpg");
image = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if ( is != null ) {
try {
is.close();
} catch (IOException ignored) { }
}
}
assertNotNull(image);
// do something with the image
}
}
现在,如何在不使用已弃用的
InstrumentationRegistry.getContext()
的情况下重写此测试?请记住
image.jpg
不是应用程序 Assets 的一部分 - 它位于
src/androidTest/assets
文件夹并打包到
AppName-buildType-androidTest.apk
(它不存在于
AppName-buildType.apk
中,我知道它的包名)。
最佳答案
我认为你应该简单地使用 InstrumentationRegistry.getInstrumentation().getContext().getAssets()
而不是 InstrumentationRegistry.getContext().getAssets()
.
它将使用你的测试上下文,所以你应该得到你的 Assets 。
关于android - InstrumentationRegistry.getContext() 替换示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63363262/
在 AndroidX 中, InstrumentationRegistry 现在已被弃用。 documentation状态 This method is deprecated. In most sce
切换到 AndroidX 并收到弃用:import androidx.test.InstrumentationRegistry。 如果我进行了下一次导入:import androidx.test.pl
尝试运行下面的测试时,我不断收到一条错误消息:'程序类型已经存在:androidx.test.InstrumentationRegistry' 我是新的 android 程序员,我不确定出了什么问题。
我正在运行 Android Instrumentation 测试,当我使用 InstrumentationRegistry.getContext() 返回的 Context 检索给定 id 的字符串时
我正在使用新的 Android 测试支持库 (com.android.support.test:runner:0.2) 来运行 Instrumentation Tests(又名设备或仿真器测试)。 我
AndroidX 迁移后,我的所有 Android 测试都扩展了 AndroidTestCase停止工作。我决定使用@RunWith(AndroidJUnit4.class)相反,但这也已被弃用,甚至
我需要从 Android 测试(AndroidJUnit4ClassRunner)中获取资源。推荐的方法是什么,为什么? String some_res_string = androidx.test.
我是一名优秀的程序员,十分优秀!