我正在尝试用Robolectric测试一项服务。碰巧服务创建了一个DB连接,检查了一些东西,然后关闭了。问题是我遇到了一个机器人错误,它找不到类。
Could not initialize class org.robolectric.shadows.ShadowSQLiteConnection
java.lang.NoClassDefFoundError: Could not initialize class org.robolectric.shadows.ShadowSQLiteConnection
at java.lang.Class.newInstance(Class.java:374)
at org.robolectric.bytecode.ShadowWrangler.createShadowFor(ShadowWrangler.java:354)
at org.robolectric.bytecode.ShadowWrangler.initializing(ShadowWrangler.java:81)
at org.robolectric.bytecode.RobolectricInternals.initializing(RobolectricInternals.java:72)
at android.database.sqlite.SQLiteConnection.$$robo$init(SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:162)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:190)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) ...
我的代码是:
@Config(manifest = "../App/AndroidManifest.xml",shadows = { MyShadowSystemClock.class})
@RunWith(RobolectricTestRunner.class)
public class DataServiceTest {
MyActivity activity;
Context context;
@Before
public void setUp() throws Exception {
ShadowLog.stream = System.out;
}
@Test
public void testServiceActionReference() throws InterruptedException {
Intent startIntent = new Intent(Robolectric.application, DataService.class);
startIntent.setAction("GET_DATA");
DataService service = new DataService();
service.onCreate();
service.onStartCommand(startIntent, 0, 42);
service.onHandleIntent(startIntent);
assertEquals(DataService.class.getCanonicalName(),startIntent.getComponent().getClassName());
service.stopService(startIntent);
service.onDestroy();
}
}
为了防止这些错误,我缺少了什么?
运行ant时,也可以在系统输出上看到这一点:
导致原因:java.lang.RuntimeException:无法提取并加载SQLite库转换为C:\Users\someone\AppData\Local\Temp\roboelectric-libs\sqlite4java.dll
您使用的是真正的旧版本吗?这似乎就是问题所在。