2014-10-27 60 views
0

我正在編寫一個android應用程序,但使用JUnit在創建活動和android特定函數之前測試了一些更具體的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

你在用什麼IDE? – Chiseled 2014-10-27 19:56:34

+0

在Ubuntu上使用Eclipse Kepler。 – 2014-10-27 20:26:37

回答

0

請確保SQLiteOpenHelper類在運行時可用於JVM。

NoClassDefFoundError通常發生在構建時需要編譯項目的類存在但運行時缺失的情況下。

確保該類包含在構建路徑和類路徑中。

+0

我的構建路徑包括Anroid 4.4.2,Android依賴關係和Android專用庫JUnit 4.我還添加了JRE庫。所以看起來我的構建路徑應該有它。類路徑是由Eclipse自動生成的,所以我認爲這些都很好? – 2014-10-27 20:25:14

+0

是的,如果你使用的是eclipse,它會自動包含類路徑中的所有類 – Chiseled 2014-10-27 20:27:32

+0

說實話,我不是一個android開發者。在運行時動態加載「SQLiteOpenHelper」類。如果是這樣的話,我們不得不明確地將它包含在類路徑 – Chiseled 2014-10-27 20:29:54

0

我通過創建一個單獨的Android測試項目並使我的類擴展了AnroidTestCase類來獲得測試工作。 https://developer.android.com/tools/testing/index.html

我知道這個答案沒有完全充實,但總之,似乎你不能使用POJO或運行常規的JUnit/JVM,因爲這些類需要Android模擬器/設計運行。

這裏是我的測試類是什麼樣子(保持在我,它在Android虛擬機上運行)

進口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 
}