编程语言
首页 > 编程语言> > 试图在MultiDex应用程序中测试Android模块,com.android.test.runner.MultiDexTestRunner无法识别

试图在MultiDex应用程序中测试Android模块,com.android.test.runner.MultiDexTestRunner无法识别

作者:互联网

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

Instrumentation runner class not specified

我的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 {
        ...
    }
}

解决方法:

我通过实现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);
    }
}

标签:android,unit-testing,junit4,multidex,android-studio-2-0
来源: https://codeday.me/bug/20190829/1761384.html