Espresso 2.0 - method annotated with @Test class inside class extending junit3 test table

I got a strange warning Method annotated with @Test inside class extending junit3 testcase when using the new ActivityInstrumentationTestCase2 class that comes with Espresso 2.0.

My class looks just like the one provided by Google as an example:

 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(); } } 

I added all the necessary things to 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' } 

Is there any way to get this warning?

+6
source share
1 answer

ActivityInstrumentationTestCase2 is a test case of JUnit 3, as it extends from TestCase .

the @Test annotation is a replacement for the naming convention for the test prefix used in JUnit 3. JUnit 4 test classes no longer require the TestCase extension or any of its subclasses. In fact, JUnit 4 tests cannot extend TestCase, otherwise AndroidJUnitRunner will consider them as JUnit 3 tests.

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

You can upgrade to the ActivityTestRule provided by com.android.support.test:rules:0.4 (or later), or stick with JUnit 3.

Another InstrumentationRegistry option provided by Espresso 2 which has getInstrumentation() , getContext() , getTargetContext() (and more). These methods provide access to the current hardware, test context, and target context in a static way. This allows you to write your own static utility methods for use in JUnit 4 test classes. These utilities will simulate the functionality currently available only in the base JUnit 3 test classes. (This is no longer necessary.)

+16
source

Source: https://habr.com/ru/post/980031/


All Articles