Android Instrumentation 测试没有从库项目中找到类

标签 android android-testing

所以当我从 Android Studio 运行我的 Activity/instrumentation 测试时,在尝试从单元测试调用库模块中的方法的代码行上,我得到了一个 NoSuchMethodError

所以这是我的测试:

public class MainActivityTest extends ActivityInstrumentationTestCase2 {
    public void testMainActivity() {
        final MainActivity target = (MainActivity) getActivity();
        MyLibarary.someStaticMethod(); // yields java.lang.NoSuchMethodError
}

这是怎么回事?我已将我的库定义为 build.gradle 中的“编译”依赖项,并且编译正常。该库也可以毫无问题地从主应用程序类中调用。只有当我从测试中调用它时它才会失败。我的应用程序和我的测试在同一个模块中。

我试过手动运行 clean 任务、assembleDebug 和 assembleDebugTest 任务。没有用。

项目结构:

Root
 |---MyApp 
 |     |---src/main/...
 |     |---src/androidTest/...
 |----MyLibrary

运行 Android Studio v1.0.2 Gradle 构建工具 v1.0.0 使用默认 Instrumentation 测试运行程序从 AS 的运行/调试配置在模块“MyApp”上作为“Android 测试”运行。

最佳答案

好吧,这个有点棘手。关键字:proguard 缩小。由于到目前为止新实现的方法被仪器测试使用,proguard 没有使用它,因此在 proguardDebugTest 构建步骤中将其从 DEX 中删除。

解决方案:要么在调试构建中禁用缩小(在 gradle 中:android.buildTypes.debug.minifyEnabled false),要么在主应用程序中使用该方法。

关于Android Instrumentation 测试没有从库项目中找到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28237696/

相关文章:

java - 尝试在 android 中的 Strings.xml 中的 textView 中设置图像

android - FragmentPagerAdapter 的 getItem() 中的 switch 语句如何工作?

android - Gradle : No tests found

android - Android 实时音频处理

android - BroadcastReceiver 监控连接的特定 USB 设备

java - Android SQLite 查询和使用游标处理多行

android - 'android.support.test.espresso does not exist' 当我想用它来进行单独的 apk 测试

gradle - 使用 Espresso 运行特定测试

android - Robolectric 使用导航架构组件,Resources$NotFoundException : nav_graph

android - 如何测试 Jetpack Compose?