Espresso 2.0 - 在扩展junit3测试用例的类中使用@Test注释的方法

Nik*_*las 6 java android android-testing android-espresso

Method annotated with @Test inside class extending junit3 testcase使用ActivityInstrumentationTestCase2Espresso 2.0附带的新类时,我收到了一个奇怪的警告.

我的课程看起来就像谷歌提供的那样:

import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.matcher.ViewMatchers.assertThat;
import static org.hamcrest.Matchers.notNullValue;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyCoolActivityTests extends ActivityInstrumentationTestCase2<MyCoolActivity> {

    private MyCoolActivity mActivity;

    public MyCoolActivityTests() {
        super(MyCoolActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        mActivity = getActivity();
    }

    @Test
    public void checkPreconditions() {
        assertThat(mActivity, notNullValue());
        // Check that Instrumentation was correctly injected in setUp()
        assertThat(getInstrumentation(), notNullValue());
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经为build.gradle添加了所​​有必要的东西:

android {
    defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个警告消失?

Jam*_*ald 16

ActivityInstrumentationTestCase2是一个JUnit 3测试用例,因为它扩展了TestCase.

@Test注释是JUnit 3中使用的测试前缀命名约定的替代.JUnit 4测试类不再需要扩展TestCase或其任何子类.实际上JUnit 4测试不能扩展TestCase,否则AndroidJUnitRunner会将它们视为JUnit 3测试.

http://developer.android.com/tools/testing-support-library/index.html#AndroidJUnitRunner

您可以迁移到(或更高版本)提供的ActivityTestRulecom.android.support.test:rules:0.4,也可以坚持使用JUnit 3.

另一种选择是InstrumentationRegistry,由咖啡2,其具有设置getInstrumentation(),getContext(),getTargetContext()(和更多).这些方法以静态方式提供对当前检测,测试上下文和目标上下文的访问.这使得编写自己的静态实用程序方法成为可能,以便在JUnit 4测试用例类中使用.这些实用程序将模仿当前仅在基本JUnit 3测试用例类中可用的功能.(这不再是必要的.)