First of all, there are similar questions like here or here 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.
My project structure looks like this:
As you can see I have a separate project for Android JUnit tests. The testing class looks like this:
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();
}
}
and here is its test case:
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);
}
}
The manifest of the test project looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.devgems.android.kurzparkzonewien.androidtests"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="net.devgems.android.kurzparkzonewien.activities" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application>
</manifest>
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: http://developer.android.com/guide/topics/manifest/instrumentation-element.html
The name of the package in my main project is
package="net.devgems.android.kurzparkzonewien.activities"
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 ADT 20.0.3, Eclipse Juno.
At last here is the logcat output:
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 -----