1

Hi i am getting the following error when trying to run my unit tests in eclipse:

java.lang.NoClassDefFoundError: android/test/AndroidTestCase
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    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.test.AndroidTestCase
    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:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 18 more

Test class

public class ResponseUtilTest extends AndroidTestCase {

    private BubbleResponseUtil responseUtil;

    private static final String TAG = ResponseUtilTest.class.getSimpleName();

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        responseUtil = new ResponseUtil();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testGetdata() {

    }

    public void testIsOkMessage() {

    }

    public void testIsRetryMessage() {

    }

    public void testIsMatchedCRC() {

    }

}

Thanks all i am using the Android sdk Eclipse edition i got from the android dev website.

Thanks again

Jono
  • 17,341
  • 48
  • 135
  • 217
  • This is definitely a duplicate - but I'm not sure of which one. There are dozens of questions about this error on SO - search for them. – Aleks G Jun 26 '13 at 10:24
  • possible duplicate of [java.lang.NoClassDefFoundError while running JUnit test in Netbeans](http://stackoverflow.com/questions/2912897/java-lang-noclassdeffounderror-while-running-junit-test-in-netbeans) – Aleks G Jun 26 '13 at 10:24
  • Not one of them actually were marked as the correct answered question – Jono Jun 26 '13 at 10:33
  • Just because an answer is not marked as accepted, it doesn't mean it's not correct. There is a big difference between "accepted" and "correct" answers. Do a search on the SO for this error - there are dozens of questions. There may be a number of reasons - and each one of them has relevant answers. – Aleks G Jun 26 '13 at 10:37
  • i did a search for this error and none of them where marked correct or even worked for my case. i am looking for correct solution/answer, not a reply. those are just replies to the given question and not one of them has indicated that it worked that provides the actual correct answer. Also that so called duplicated question was in regarding using netbeans IDE. i guess you did not even read my question at all – Jono Jun 26 '13 at 10:39
  • That doesnt work for me or many others due to the fact that junit is built in the android sdk lib so you wont see it seperately in your classpath – Jono Jun 26 '13 at 10:53

2 Answers2

3

You'll need to run your AndroidTestCase towards the device/emulator. I'm guessing you're just running it as a regular JUnit3/JUnit4 test case?

karllindmark
  • 6,031
  • 1
  • 26
  • 41
  • yes i am running it as a regular JUNIT3/4 . how do i run it on an emulator? – Jono Jun 26 '13 at 10:32
  • 1
    @jonney Start the emulator, or connect a device. In Package Explorer right-click the test project and select "Run As -> Android JUnit Test". It will install the tests on the device and run them (which is kind of cool!). – Stephen Hosking Nov 26 '13 at 02:57
0

ninetwozero is correct. There is no device for the AndroidTestCase to run on. You either need to attach a real device or create an Android Virtual Device (emulator) with the characteristics of your device.

Right click the project with your tests in it, then Run As->Run Configurations. Select Android JUnit Test, click "New Launch Configuration" button. Go to "Target" tab; use "Manager" button on bottom right to create your new emulator. I'm sure other articles cover how to set up an emulator.

DanS
  • 1
  • 1