gpt4 book ai didi

AndroidJUnitRunner 给出 'java.lang.ClassNotFoundException' 的 Android 概率

转载 作者:行者123 更新时间:2023-11-29 19:15:20 27 4
gpt4 key购买 nike

我是仪器测试的新手。我正在尝试使用 AndroidJUnitRunner 进行基本测试。这是我的毕业典礼:

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.kaushik.mycart.ui"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
jackOptions.enabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

..........................
..........................

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'

}

然后我添加了一个名为“ProductListActivityTest”的类来使用 AndroidJunitRunner 进行测试。如下:

package com.kaushik.myredmart.ui;
// all includes
@RunWith(AndroidJUnit4.class)
public class ProductListActivityTest {

@Rule
public ActivityTestRule<ProductListActivity> rule = new ActivityTestRule<>(ProductListActivity.class);

@Test
public void ensureListViewIsPresent() throws Exception {
ProductListActivity activity = rule.getActivity();
View viewByIdw = activity.findViewById(R.id.productListView);
assertThat(viewByIdw,notNullValue());
assertThat(viewByIdw, instanceOf(RecyclerView.class));
RecyclerView productListView = (RecyclerView) viewByIdw;
RecyclerView.Adapter adapter = productRecyclerView.getAdapter();
assertThat(adapter, instanceOf(ProductAdapter.class));

}
}

我没有在“androidTest”中添加其他文件。我还想提一下,我的源代码中有应用程序类文件。现在每次我尝试运行测试时,都会出现以下错误:

Started running tests
Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
Empty test suite.

谁能帮我找出测试代码中的问题?

最佳答案

您如何运行测试?

如果您是从 Android Studio 执行此操作:

  • 打开运行菜单 -> 编辑配置
  • 添加新的 Android 测试配置
  • 选择一个模块
  • 添加特定的检测运行器:

    android.support.test.runner.AndroidJUnitRunner

通过 Gradle 从命令行:

./gradlew connectedAndroidTest

关于AndroidJUnitRunner 给出 'java.lang.ClassNotFoundException' 的 Android 概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43726015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com