Note that there are some explanatory texts on larger screens.

plurals
  1. PONoClassDefFoundError while executing Android JUnit test
    text
    copied!<p>First of all, there are similar questions like <a href="https://stackoverflow.com/questions/5758966/java-lang-noclassdeffounderror-in-android-test-servicetestcase">here</a> or <a href="https://stackoverflow.com/questions/2247998/noclassdeffounderror-eclipse-and-android">here</a> but I have neither some external libs nor a lib folder nor some included jars or something, so I wonder what I'm doing wrong by running Android Junit tests. </p> <p>My project structure looks like this:<br> <img src="https://i.stack.imgur.com/84g1N.jpg" alt="enter image description here"></p> <p>As you can see I have a separate project for Android JUnit tests. The testing class looks like this: </p> <pre><code>public class PersistenceManager { private static final String FILENAME = "kpzwien_storage"; public static void persist(String data, Context context) throws IOException { FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(data.getBytes()); fos.close(); } public static String read(Context context) throws IOException { FileInputStream fis = context.openFileInput(FILENAME); StringBuffer fileContent = new StringBuffer(""); byte[] buffer = new byte[1024]; while (fis.read(buffer) != -1) { fileContent.append(new String(buffer)); } return fileContent.toString(); } } </code></pre> <p>and here is its test case: </p> <pre><code>public class PersistenceManagerTest extends AndroidTestCase { private final String FILENAME_PREFIX = "test."; @Before public void setUp() { MockContentResolver resolver = new MockContentResolver(); RenamingDelegatingContext renamingDelegatingContext = new RenamingDelegatingContext(new MockContext(), getContext(), FILENAME_PREFIX); Context context = new IsolatedContext(resolver, renamingDelegatingContext); setContext(context); } public void testPersistAndRead() throws IOException { String testData = "foobar"; PersistenceManager.persist(testData, getContext()); String result = PersistenceManager.read(getContext()); assertEquals(testData, result); } } </code></pre> <p>The manifest of the test project looks like this: </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.devgems.android.kurzparkzonewien.androidtests" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="4" /&gt; &lt;instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="net.devgems.android.kurzparkzonewien.activities" /&gt; &lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" &gt; &lt;uses-library android:name="android.test.runner" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>targetPackage is the application that the Instrumentation object will run against which is is identified by the package name assigned in its manifest file by the element. (Source: <a href="http://developer.android.com/guide/topics/manifest/instrumentation-element.html" rel="nofollow noreferrer">http://developer.android.com/guide/topics/manifest/instrumentation-element.html</a></p> <p>The name of the package in my main project is </p> <blockquote> <p>package="net.devgems.android.kurzparkzonewien.activities"</p> </blockquote> <p>So it is exactly where targetPackage points to. If I run as "Android JUnit test", I always get a NoClassDefFoundError but why? Any ideas? I'm using <strong>ADT 20.0.3</strong>, <strong>Eclipse Juno</strong>.</p> <p>At last here is the logcat output: </p> <blockquote> <p>09-22 16:00:12.745: I/TestRunner(1528): java.lang.NoClassDefFoundError: net.devgems.android.kurzparkzonewien.controller.PersistenceManager 09-22 16:00:12.745: I/TestRunner(1528): at net.devgems.android.kurzparkzonewien.androidtests.PersistenceManagerTest.testPersistAndRead(PersistenceManagerTest.java:32) 09-22 16:00:12.745: I/TestRunner(1528): at java.lang.reflect.Method.invokeNative(Native Method) 09-22 16:00:12.745: I/TestRunner(1528): at java.lang.reflect.Method.invoke(Method.java:521) 09-22 16:00:12.745: I/TestRunner(1528): at junit.framework.TestCase.runTest(TestCase.java:154) 09-22 16:00:12.745: I/TestRunner(1528): at junit.framework.TestCase.runBare(TestCase.java:127) 09-22 16:00:12.745: I/TestRunner(1528): at junit.framework.TestResult$1.protect(TestResult.java:106) 09-22 16:00:12.745: I/TestRunner(1528): at junit.framework.TestResult.runProtected(TestResult.java:124) 09-22 16:00:12.745: I/TestRunner(1528): at junit.framework.TestResult.run(TestResult.java:109) 09-22 16:00:12.745: I/TestRunner(1528): at junit.framework.TestCase.run(TestCase.java:118) 09-22 16:00:12.745: I/TestRunner(1528): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169) 09-22 16:00:12.745: I/TestRunner(1528): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154) 09-22 16:00:12.745: I/TestRunner(1528): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430) 09-22 16:00:12.745: I/TestRunner(1528): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447) 09-22 16:00:12.745: I/TestRunner(1528): ----- end exception -----</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload