【问题标题】:Trying to test an Android module in MultiDex app, com.android.test.runner.MultiDexTestRunner is not recognized尝试在 MultiDex 应用程序中测试 Android 模块,com.android.test.runner.MultiDexTestRunner 无法识别
【发布时间】:2016-03-22 11:38:49
【问题描述】:

有人知道为什么我不能使用 MultiDexTestRunner 吗?

我的 build.gradle 包含:

android {
    ...
    defaultConfig {
        ...
        multiDexEnabled true

        testInstrumentationRunner 'com.android.test.runner.MultiDexTestRunner'
    }

dependencies {
...
    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'
    androidTestCompile 'junit:junit:4.12'

Gradle 的 Android 插件版本为 2.0.0-alpha2 (com.android.tools.build:gradle:2.0.0-alpha2)

//Runs all unit tests.
@RunWith(Suite.class)
@Suite.SuiteClasses({ACLTest.class})
public class UnitTestSuite {

    public UnitTestSuite() {}
}

测试类:

@RunWith(AndroidJUnit4.class)
@MediumTest
public class ACLTest {

    static Context context;

    public ACLTest() {
    }

    @BeforeClass
    public static void setUpBeforeClass() {
        ...
        context = InstrumentationRegistry.getTargetContext();
    }

    @Before
    public void setUpBeforeTest() throws Exception {
      ...
    }


    @After
    public void tearDown() throws Exception {
        ...
    }

    @Test
    public void aAGetInstance() throws Exception {
        ...
    }
}

【问题讨论】:

  • 也许您的应用程序应该从 dex-support 库扩展 MultiDexApplication>?
  • @OrestSavchak 应用程序已经这样做了。但我想做的是在项目的一个模块中使用单元测试。

标签: android unit-testing junit4 android-multidex android-studio-2.0


【解决方案1】:

我通过实现 android.support.test.runner.AndroidJUnitRunner 的扩展并覆盖 onCreate 解决了我的问题

public class MultiDexAndroidJUnitRunner extends AndroidJUnitRunner {
    @Override
    public void onCreate(Bundle arguments) {
        //To make it work on MultiDex environment.
        //https://plus.google.com/+OleksandrKucherenko/posts/i7qZdVEy3Ue
        MultiDex.install(getTargetContext());

        super.onCreate(arguments);
    }
}

【讨论】:

  • 酷。但是如何使用呢?我正在尝试在@RunWith 注释中使用它,但它不起作用/
  • 我找到了。我需要在 build.gradle 的 testInstrumentationRunner 中设置它。
  • 我在相同的上下文中,但是使用 Espresso,这并不能解决问题:我得到一个:app:transformClassesWithMultidexlistForDebugAndroidTest FAILED。有什么提示吗? @tse
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2023-02-15
  • 1970-01-01
  • 2012-11-21
  • 2018-11-04
相关资源
最近更新 更多