问题
This is my ApplicationTest.java
package com.example.testing;
import android.app.Application;
import android.test.ApplicationTestCase;
import com.example.agent.database.MyGroupsTable;
import com.orm.SugarContext;
import java.util.List;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
static final String DB_NAME = "database.db";
public ApplicationTest() {
super(Application.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
boolean deleted = mContext.deleteDatabase(DB_NAME);
SugarContext.init(mContext);
}
/**
* :Check: tables created or not
*/
public void testDatabase() {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
MyGroupsTable table = new MyGroupsTable();
long i = table.save();
assertTrue("Failed check table", i >= 0);
} else {
MyGroupsTable i = MyGroupsTable.findById(MyGroupsTable.class, (long) 0);
long j = MyGroupsTable.save(new MyGroupsTable("1", "testing", "description", "avatar"));
assertTrue("Failed to create table", j < 0);
List<MyGroupsTable> my = MyGroupsTable.find(MyGroupsTable.class, " GROUPID = ?", "1");
if (my.size() > 0) {
assertTrue(true);
} else {
assertTrue(false);
}
assertTrue("Failed to get results from MyGroupsTable", my.get(0).getGroup_id().equals("1"));
}
}
}
I am using SugarORM. Which is working fine when running the app as debug or release but when i tries to execute Instrumentation test it blows up with Exception
This is the Stack Trace Testing started at 4:36 PM ...
06/16 16:36:44: Launching ApplicationTest
No apk changes detected since last installation, skipping installation of /Users/sandeeprana/StudioProjects/iliad_agent/app/build/outputs/apk/app-debug.apk
$ adb shell am force-stop com.loktra.iliadagent
$ adb push /Users/sandeeprana/StudioProjects/iliad_agent/app/build/outputs/apk/app-debug-androidTest-unaligned.apk /data/local/tmp/com.loktra.iliadagent.test
$ adb shell pm install -r "/data/local/tmp/com.loktra.iliadagent.test"
pkg: /data/local/tmp/com.loktra.iliadagent.test
Success
Running tests
$ adb shell am instrument -w -r -e debug false -e class com.example.agent.ApplicationTest com.example.agent.test/com.android.test.runner.MultiDexTestRunner
Client not ready yet..Test running started
android.database.sqlite.SQLiteException: no such table: MY_GROUPS_TABLE (code 1): , while compiling: INSERT OR REPLACE INTO MY_GROUPS_TABLE(ID,GROUPDESCRIPTION,GROUPNAME,GROUPID,GROUPAVATAR) VALUES (?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1573)
at com.orm.SugarRecord.save(SugarRecord.java:266)
at com.orm.SugarRecord.save(SugarRecord.java:360)
at com.loktra.iliadagent.ApplicationTest.testDatabase(ApplicationTest.java:40)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1700)
Tests ran to completion.
The code is working fine on pre-Lollipop and post Lollipop devices while running as debug/release. But i try to execute Instrumentation Testing. It throws no such Table Exception.
回答1:
From ref : https://github.com/satyan/sugar/issues/613#issuecomment-223300514
Create a class:
public class App extends SugarApp {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this);
}
@Override
public void onTerminate() {
SugarContext.terminate();
super.onTerminate();
}
}
Then specify it in AndroidManifest.xml
<application
...
android:name=".App">
...
</application>
Uninstall app, disable instarun, Then run again.It worked for me.
来源:https://stackoverflow.com/questions/37857898/sugarorm-while-instrumentation-testing-no-such-table-exception