Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-ProviderTestCase2 with API Level 28 not working

Some time ago I created some instrumented tests using ProviderTestCase2. Now I wanted to update my codebase and tried to reactivate my old tests in the current environment.

1) When I update to compileSdkVersion 28 there seems to be no class ProviderTestCase2 any more. Cleaning and building the Project failed. If I go back to version 27 I can successfully build the project.

2) I have a special case where I want to test different variants of an object with a set of tests. So I decided to make use of inheritance and placed the tests in the base class, configuring the objects to be tested by the superclass. This had worked in the past. (Yes, I know about opinions that inheritance in tests is bad, but in this case it is OK ;-) )

Now no tests are executed. Android Studio complains that there is an empty test suite.

Also there are warnings that I have tests with JUnit4 Annotations in a class extending JUnit3. I do not remember that I saw these warnings in the past.

Is this a bug in the Android Libraries? Or where can I find some hints how to solve this problem?

(Using Android Studio 3.2 and the currentmost libraries)

like image 366
Sqrt-1764 Avatar asked Oct 01 '18 19:10

Sqrt-1764


2 Answers

The ProviderTestCase2 was removed. The ProviderTestRule should be used instead (from support test library or AndroidX).

See:

  • Android Developers Blog: Android Testing Support Library 1.0 is here! (see heading ProviderTestRule) and
  • Android Developer Reference: ProviderTestRule

Add the com.android.support.test:rules dependency, to be able to use the rule.

Here is an example from the reference page:

@Rule
public ProviderTestRule mProviderRule =
    new ProviderTestRule.Builder(MyContentProvider.class, MyContentProvider.AUTHORITY).build();

@Test
public void verifyContentProviderContractWorks() {
    ContentResolver resolver = mProviderRule.getResolver();
    // perform some database (or other) operations
    Uri uri = resolver.insert(testUrl, testContentValues);
    // perform some assertions on the resulting URI
    assertNotNull(uri);
}
like image 51
WebDucer Avatar answered Sep 27 '22 17:09

WebDucer


The accepted answer is not correct. ProviderTestRule being in Beta, there is no deprecation or removal of ProviderTestCase2. What happened is that is has been moved to a library that you need to declare in your build.gradle:

useLibrary 'android.test.base'

See: https://developer.android.com/training/testing/set-up-project

like image 36
mtotschnig Avatar answered Sep 27 '22 17:09

mtotschnig