views:

54

answers:

1

I'm working on an Android app where some part of it gets JSON-formatted data from a web service. The basics of the class parsing the JSON data looks like this:

public class JsonCourseParser implements CourseParser
{
 public Course parseCourse(String courseData)
 {
  Course result;
  try
  {
   JSONObject jsonObject = new JSONObject(courseData);
   // parse stuff here and construct a proper Course object
   result = new Course("foo", "bar");
  }
  catch (JSONException e)
  {
   result = null;
  }

  return result;
 }
}

This builds just fine, and it works when I call the parseCourse() method from within my Activity.

When trying to test this very same function in a unit test however, the unit test won't even launch. A NoClassDefFoundError is shown in the Failure Trace of the JUnit view in Eclipse:

java.lang.NoClassDefFoundError: org/json/JSONException
 at JsonCourseParserTest.initClass(JsonCourseParserTest.java:15)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
 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: org.json.JSONException
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 ... 16 more

My unit test looks like this:

public class JsonCourseParserTest
{
 private static CourseParser courseParser;

 @BeforeClass
 public static void initClass()
 {
  courseParser = new JsonCourseParser();
 }

 @Test
 public void testParseCourse()
 {
  Object course = courseParser.parseCourse(DummyJsonConstants.TEP4165_JSON);
  assertNotNull(course);
 }
}

If I simply comment out the line with the JSONObject (and the belonging JSONException) in my JsonCourseParser class, the test runs just fine.

In case it might be relevant, I have the following structure on my workspace:
I have three different projects. One is the Android application project. The second is an Android Test project where I have unit tests that needs to interact with application context, database, views etc. The third is a normal project where I keep the "clean" unit tests, i.e. those that only test utility classes, POJO classes etc. The unit test above resides in the third project.

Edit:
I sadly don't have much more detail to add to the problem description. Is there really nobody that has any idea what might cause this issue? Any suggestions would be welcome!

A: 
Nailuj