loo*_*e11 3 testing android robolectric sugarorm
我试图为我的android项目设置测试环境.基本的Robolectric设置完成.我使用了这个很好的教程.如果我在Manifest.xml中注释掉SugarORM,那么所有测试都可以.但如果我想与SugarORM一起使用,我总是会遇到这个错误:
来自com.orm.SugarDb.createDatabase的com.orm.SugarDb.getDomainClasses(SugarDb.java:37)的dalvik.system.DexFile $ DFEnum.hasMoreElements(DexFile.java:239)中的java.lang.NullPointerException(SugarDb.java: 104)位于android.database.sqlite.SQLiteOpenper.getWritableDatabase(SQLiteOpenHelper.java:164)的android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)的com.orm.SugarDb.onCreate(SugarDb.java:100) )com.orm.Database.getDB(Database.java:18)at com.orm.SugarApp.onTerminate(SugarApp.java:16)atg.robolectric.internal.ParallelUniverse.tearDownApplication(ParallelUniverse.java:133)at org .robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:246)org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)org.junit.runners.在org.junit的org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)的org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)中的ParentRunner $ 1.schedule(ParentRunner.java:71) .runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:158)org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org. junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)57)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner) .java:288)org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at org.robolectric.RobolectricTestRunner $ 1.evaluate (RobolectricTestRunner.java:158)org.junit.runners.ParentRunner.run(ParentRunner.java:363)位于com.intellij.rt.execution的org.junit.runner.JUnitCore.run(JUnitCore.java:137). application.AppMain.main(AppMain.java:134)57)org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at org.junit.runners.ParentRunner.runChildren(ParentRunner) .java:288)org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at org.robolectric.RobolectricTestRunner $ 1.evaluate (RobolectricTestRunner.java:158)org.junit.runners.ParentRunner.run(ParentRunner.java:363)位于com.intellij.rt.execution的org.junit.runner.JUnitCore.run(JUnitCore.java:137). application.AppMain.main(AppMain.java:134)ParentRunner.access $ 000(ParentRunner.java:58)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)位于org.junit的org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:158). runners.ParentRunner.run(ParentRunner.java:363)atg.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)ParentRunner.access $ 000(ParentRunner.java:58)org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)位于org.junit的org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:158). runners.ParentRunner.run(ParentRunner.java:363)atg.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
每个人都有同样的问题吗?
编辑
也许是这样,因为Robolectric在测试后关机并且还没有完成.我的TestClass
@RunWith(RobolectricTestRunner.class)
@Config(manifest="./src/main/AndroidManifest.xml",emulateSdk=18)
public class MyActivityTest{
private ActivityController<OnLogActivity> controller = Robolectric.buildActivity(OnLogActivity.class).create().setup();
@Test
public void clickingLogin_shouldStartLoginActivity() {
OnLogActivity activity = controller.get();
assertTrue(activity.findViewById(R.id.login) instanceof Button);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2.0:
Robolectric可以找到android:name=com.orm.SugarApp你必须使用相同的com.orm包创建一个测试文件夹,并添加一个名为TestSugarApp的测试类.之后你可以测试所有的东西.
package com.orm;
...
@RunWith(RobolectricTestRunner.class)
@Config(manifest="./src/main/AndroidManifest.xml",emulateSdk=18)
public class TestSugarApp extends Application
implements TestLifecycleApplication {
private ActivityController<OnLogActivity> controller;
@Test
public void startEverTestSugarAppAsFirst() {
}
@Override
public void beforeTest(Method method) {
Log.v("test", "beforeTest");
}
@Override
public void prepareTest(Object test) {
Log.v("test", "prepareTest");
}
@Override
public void afterTest(Method method) {
Log.v("test", "afterTest");
}
}
Run Code Online (Sandbox Code Playgroud)
好的,接下来.将下一个类添加到测试代码中:
public class TestSugarApp
extends SugarApp
{
@Override
public void onCreate() {}
@Override
public void onTerminate() {}
}
Run Code Online (Sandbox Code Playgroud)
名为Test的类将由Robolectric加载和使用,您可以覆盖一些与测试无关的内容.我试图阻止从执行代码SugarApp中onCreate和onTerminate(https://github.com/satyan/sugar/blob/master/library/src/com/orm/SugarApp.java).