相关疑难解决方法(0)

在Android junit测试用例中获取测试项目的上下文

有谁知道如何在Android junit测试用例中获得Test项目的上下文(扩展AndroidTestCase).

注意:测试不是仪器测试.

注2:我需要测试项目的上下文,而不是测试的实际应用程序的上下文.

我需要这个从测试项目的资产加载一些文件.

junit android

100
推荐指数
8
解决办法
6万
查看次数

不推荐使用androidx.test.InstrumentationRegistry

切换到AndroidX并收到已弃用:import androidx.test.InstrumentationRegistry

如果我下次导入:import androidx.test.platform.app.InstrumentationRegistry 我不能getContext()

例如: val context = InstrumentationRegistry.getContext()

在build.gradle中:

androidTestImplementation 'androidx.test.ext:junit:1.0.0-beta02'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
Run Code Online (Sandbox Code Playgroud)

android deprecated androidx

34
推荐指数
4
解决办法
1万
查看次数

访问android测试项目中的资源

我已经设置了一个运行junit测试的android测试项目.它正在使用两个eclipse项目"Application"和"ApplicationTest",我的测试在"ApplicationTest"项目中.在我的一个测试中,我需要访问一个文件,如果我把文件放在SD卡上并指向一个File对象,这个工作正常.但是我想将该文件作为资源访问,但这似乎不起作用.这就是我做的:

  • 保存文件 ApplicationTest/res/raw/myfile.xml
  • 试图使用它: InputStream is = getContext().getResources().openRawResource(R.raw.myfile);

但这给了我这个例外:

android.content.res.Resources$NotFoundException: File Hello World, HelloAndroidActivity! from drawable resource ID #0x7f040000
at android.content.res.Resources.openRawResource(Resources.java:823)
at android.content.res.Resources.openRawResource(Resources.java:799)
at com.quizzer.test.QuestionHandlerTests.testImportQuestions(Tests.java:182)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Caused by: java.io.FileNotFoundException: Hello World, HelloAndroidActivity!
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:406)
at android.content.res.Resources.openRawResource(Resources.java:820)
... 14 more

我的测试类扩展了AndroidTestCase,这就是上下文的来源.

更新:

所以问题似乎是在编译期间使用了测试项目中的资源,但是在运行时使用了主项目中的资源.我还不确定如何解决这个问题.因此,目前只有在测试项目和主项目中放置相同的原始资源时才有效,这当然是非常愚蠢的.

eclipse android unit-testing

19
推荐指数
1
解决办法
2万
查看次数

Android Studio上的单元测试:"未模拟"错误

我是Android Studio新手.我使用的是Android Studio 1.2预览版2,gradle 2.2.1和gradle插件1.1.0.

在尝试运行我的单元测试时,我无法解决此错误:

java.lang.RuntimeException: Method getInstrumentation in android.test.InstrumentationTestCase not mocked
Run Code Online (Sandbox Code Playgroud)

这是我的测试类:

public class AppPreferencesTest extends InstrumentationTestCase {

AppPreferences preferences;

@Before
public void setUp() throws Exception {
    preferences = new AppPreferences(getInstrumentation().getTargetContext());
}

...
Run Code Online (Sandbox Code Playgroud)

在我的build.gradle中:

testCompile 'junit:junit:4.12'
Run Code Online (Sandbox Code Playgroud)

我尝试添加这个

testOptions { 
    unitTests.returnDefaultValues = true
}
Run Code Online (Sandbox Code Playgroud)

因为我在http://tools.android.com/tech-docs/unit-testing-support所遵循的步骤中提到过, 但它没有解决它.

我也试过创建一个MockContext:

preferences = new AppPreferences(new MockContext());
Run Code Online (Sandbox Code Playgroud)

但AppPreferences的构造函数比给出错误

public AppPreferences(Context context) {
    preferences = PreferenceManager.getDefaultSharedPreferences(
            context);
}
Run Code Online (Sandbox Code Playgroud)

...

RuntimeException: Method getDefaultSharedPreferences in android.preference.PreferenceManager not mocked.
Run Code Online (Sandbox Code Playgroud)

android unit-testing android-studio

13
推荐指数
2
解决办法
3万
查看次数

Android Studio Unit测试SQLiteDataBase为null

我是单元测试的新手,我想测试我的SQLiteDataBase.

我有一个名为MySQLiteHelper的类,它扩展了SQLiteOpenHelper.我有一个名为LocationDataHandler的类,我用它来添加或删除我的DataBase中的元素.我有一个类LocationDataHandlerTest,它扩展了AndroidTestCase,以测试我的LocationDataHandler类.

我正在尝试测试我的SQLite数据库,但我似乎在所有不同的上下文之间有点迷失.

这是我的代码:

//Context context = new Activity();

//IsolatedContext context = getMockContext();

//Context context = new MockContext();

//Context context = getInstrumentation().getContext();

  Context context = getContext();

  helper = new MySQLiteHelper(context);
  assertNotNull(helper);

  SQLiteDatabase db = helper.getWritableDatabase();
  assertNotNull(db); <---- it fails here !
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我尝试过许多不同的环境,我看到人们使用和使用它们.我真的不明白为什么它在我的情况下不起作用.

我的问题是测试在"assertNotNull(db);"行上失败了 这意味着我永远无法检索我的数据库.

我这样做是错误的吗?我没有使用正确的背景吗?

编辑:这是我的logcat:

junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:55)
    at junit.framework.Assert.assertTrue(Assert.java:22)
    at junit.framework.Assert.assertNotNull(Assert.java:256)
    at junit.framework.Assert.assertNotNull(Assert.java:248)
    at junit.framework.TestCase.assertNotNull(TestCase.java:417)
    at com.databerries.LocationDataHandlerTest.testAddLocation(LocationDataHandlerTest.java:72)
Run Code Online (Sandbox Code Playgroud)

java sqlite android unit-testing

9
推荐指数
1
解决办法
2723
查看次数