Java – espresso 2.0 – methods using @ test annotation in classes that extend junit3 testcase
When I use the new activityinstrumentationtestcase2 class shipped with espresso 2.0, I get a strange warning method. I use the @ test internal class to extend junit3 testcase
My course looks like what Google offers:
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've done it for build Gradle adds everything necessary:
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 make this warning disappear?
Solution
Activity instrumentation testcase 2 is a JUnit 3 test case because it extends from testcase
http://developer.android.com/tools/testing-support-library/index.html#AndroidJUnitRunner
You can migrate to by com android. support. Activitytestrule provided by test: rule: 0.4 (or higher), or insist on using JUnit 3
Another option is instrumentationregistry, which is provided by espresso 2. It has getinstrumentation(), getcontext(), gettargetcontext() (and more) These methods provide static access to the current detection, test context and target context This makes it possible to write your own static utility methods for use in JUnit 4 test case classes These utilities will mimic the functionality currently available only in the basic JUnit 3 test case class (this is no longer necessary.)