问题描述
这是我的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"));
}
}
}
我正在使用SugarORM.当以调试或发行版本的形式运行应用程序时,这工作正常,但是当我尝试执行Instrumentation测试时,它会因异常而爆炸
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
这是堆栈跟踪 测试从下午4:36开始...
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)
测试完成.
该代码在Lollipop之前和之后的Lollipop设备上以调试/发行版运行时均能正常工作.但是我尝试执行仪器测试.它不会引发此类表异常.
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.
推荐答案
从ref: https://github.com/satyan/sugar/issues/613#issuecomment-223300514
创建一个类:
public class App extends SugarApp {
@Override
public void onCreate() {
super.onCreate();
SugarContext.init(this);
}
@Override
public void onTerminate() {
SugarContext.terminate();
super.onTerminate();
}
}
然后在AndroidManifest.xml中指定它
Then specify it in AndroidManifest.xml
<application
...
android:name=".App">
...
</application>
卸载应用程序,禁用instarun,然后再次运行.它对我有用.
Uninstall app, disable instarun, Then run again.It worked for me.
这篇关于SugarORM:在进行仪器测试时,没有这样的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!