dimanche 24 avril 2016

Tests for Android Content Provider strange behavior

I wrote a ContentProvider to deal with query/insert/update/delete on a local sqlite database.

For testing purposes I wrote couple of tests:

TestMovieContract.java : for testing MovieContract class
TestMovieDBHelper.java : for testing MovieDBHelper class
TestUriMatcher.java    : for testing UriMatcher inside MovieContentProvider class
TestProvider.java      : for testing MovieContentProvider class

When I run each of these tests separately, they all get passed but when I run all of them one of the tests fails. The assert statement which it fails is:

returnedUri = mContext.getContentResolver().insert(
         movieInsertUri,
         sampleMovieValues
  );

assertNotNull("the values have not been inserted into " + MovieContract.MovieEntry.TABLE_NAME, returnedUri);

When I checked the logcat I saw an exception when executing this specific test:

04-24 19:16:00.086 6938-6963/com.example.android.popularmovies.app I/TestRunner: started: testAndroidTestCaseSetupProperly(com.example.android.popularmovies.app.data.TestProvider)
04-24 19:16:00.104 6938-6963/com.example.android.popularmovies.app I/TestRunner: finished: testAndroidTestCaseSetupProperly(com.example.android.popularmovies.app.data.TestProvider)
04-24 19:16:00.104 6938-6963/com.example.android.popularmovies.app I/TestRunner: passed: testAndroidTestCaseSetupProperly(com.example.android.popularmovies.app.data.TestProvider)
04-24 19:16:00.104 6938-6963/com.example.android.popularmovies.app I/TestRunner: started: testDelete(com.example.android.popularmovies.app.data.TestProvider)
04-24 19:16:00.122 6938-6963/? E/SQLiteLog: (1032) statement aborts at 33: [INSERT INTO movie(popularity,vote_average,original_title,backdrop_url,movie_id,original_lang,image_url,overview,title,is_adult,release_date,vote_count,is_favorite,genre_ids) VALUES (?,
04-24 19:16:00.122 6938-6963/? E/SQLiteDatabase: Error inserting popularity=8.3 vote_average=5.2 original_title=Some Android backdrop_url=http://ift.tt/1rsRGeB movie_id=40443 original_lang=en image_url=http://ift.tt/24dIBnz overview=This is a sample overview title=Very First Android is_adult=true release_date=2015-05-23 vote_count=33345 is_favorite=true genre_ids=23,1,5
                                                 android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)
                                                     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
                                                     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:780)
                                                     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
                                                     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
                                                     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
                                                     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
                                                     at com.example.android.popularmovies.app.data.MovieContentProvider.insert(MovieContentProvider.java:197)
                                                     at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)
                                                     at android.content.ContentResolver.insert(ContentResolver.java:1231)
                                                     at com.example.android.popularmovies.app.data.TestProvider.testDelete(TestProvider.java:308)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at junit.framework.TestCase.runTest(TestCase.java:168)
                                                     at junit.framework.TestCase.runBare(TestCase.java:134)
                                                     at junit.framework.TestResult$1.protect(TestResult.java:115)
                                                     at junit.framework.TestResult.runProtected(TestResult.java:133)
                                                     at junit.framework.TestResult.run(TestResult.java:118)
                                                     at junit.framework.TestCase.run(TestCase.java:124)
                                                     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
                                                     at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
                                                     at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
                                                     at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
04-24 19:16:00.122 6938-6963/? W/SQLiteLog: (28) file unlinked while open: /data/user/0/http://ift.tt/1rsRHPB

There is a warning at the end of this log which I think is important. It shows the database file unlinked. I checked all of my codes to see the proper closure of any database connection.

My question is why the test passes when I just run TestProvider.java but fails when I run all of them? and how can I solve it?

Aucun commentaire:

Enregistrer un commentaire