我正在编写一个Android应用程序但是在创建活动和特定于android的函数之前使用JUnit来测试一些更具特定于java的功能。在我写出JUnit测试并逐位执行时,我发现了dbHelper = new DatabaseSystemHelper(context, null, null, 0);
public DatabaseSystem(Context context) {
dbHelper = new DatabaseSystemHelper(context, null, null, 0);
database = dbHelper.getWritableDatabase();
}
class DatabaseSystemHelper extends SQLiteOpenHelper {
...
DatabaseSystemHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
setDatabaseValues(context);
}
**java.lang.NoClassDefFoundError: android/database/sqlite/SQLiteOpenHelper**
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at edu.project2.systems.DatabaseSystem.<init>(DatabaseSystem.java:58)
at edu.project2.systems.DatabaseSystem$SingletonHolder.<clinit>(DatabaseSystem.java:193)
at edu.project2.systems.DatabaseSystem.getInstance(DatabaseSystem.java:203)
at edu.project2.systems.DatabaseSystemTest.setUp(DatabaseSystemTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: android.database.sqlite.SQLiteOpenHelper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 42 more
我有点失落。似乎我的代码永远不会超过创建新DatabaseHelper对象的界限。事实上,当我“步入”调用时,调试游标甚至不会进入构造函数。任何建议都会很棒
答案 0 :(得分:0)
请确保运行时JVM可以使用SQLiteOpenHelper类。
NoClassDefFoundError通常发生在编译项目所需的类在构建期间存在但在运行时丢失时。
确保类包含在构建路径和类路径中。
答案 1 :(得分:0)
我通过创建一个单独的Android测试项目并使我的类扩展AnroidTestCase类来使测试工作。 https://developer.android.com/tools/testing/index.html
我知道这个答案并没有完全充实,但简而言之,当您处理特定于Android的类时,您似乎无法使用POJO或运行常规JUnit / JVM,因为这些类需要Android模拟器/设计运行。
这是我的测试类看起来的样子(保持它在android VM中运行)
import android.content.Context; import android.test.AndroidTestCase;
public class DatabaseSystemsTest extends AndroidTestCase {
private DatabaseSystem dbSystem;
@Override
protected void setUp() throws Exception {
super.setUp();
Context context = getContext();
DatabaseSystem.setContext(context);
dbSystem = DatabaseSystem.getInstance();
dbSystem.getDbHelper().onCreate(dbSystem.getDatabase());
} // end of setUp() method definition
}