Essej
Essej

Reputation: 861

Testning with Android Studio: NoClassDefFoundError: org/testng/TestNG

So I am trying to use the built in testTool to generate some test for my mainActivity, so far I have managed to generate a test class and I got empty methods in it to test the methods of mainActivity. If I run the testClass with coverage I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 18 more

I'm not sure what this really means, there is a class that is not defined?

I might also add that I added:

    task testClasses {
    doLast {
        println 'This is a dummy testClasses task'
    }
}

in the build.gradle file for my sqlite module, otherwise I couldn't run any test at all.

I might also add that under Dependencies tab under Module settings, I do have "org.testng:testnb:6.9.6" set to "Test compile".

This is how my dependencies look in my gradle file:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    androidTestCompile 'org.testng:testng:6.9.6'
    compile project(':sqlite-jdbc-3.8.11.2')
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.getbase:floatingactionbutton:1.10.1'
    compile 'com.android.support:design:23.3.0'
    compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.numetriclabz.numandroidcharts:numandroidcharts:1.0.13'
}

EDIT: I saw that Android studio also created an ExampleUnitTest file and If I add my testcases to this file and run them, it works just fine. The problem only seems to happen when I try to run the whole MainActivityTest class, but individual methods seems to be ok to test.

Upvotes: 2

Views: 1688

Answers (2)

Anton
Anton

Reputation: 1502

Check your module structure. Probably one of your high level module folders doesn`t have dependencies. (You can link modules together and everything should work).

Go to -> File|Project structure|Modules|*your module*|Dependencies. It's not a TestNG issue, it's a module structure issue.

More detailed description Click This -> Link

Upvotes: 1

Evan M
Evan M

Reputation: 240

Try changing

testCompile 'junit:junit:4.12'

to

androidTestCompile 'junit:junit:4.12'

Also, make sure you are running it as an Android Test.

Upvotes: 0

Related Questions