51212

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

Question:

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.

Answer1:

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 -> <a href="https://youtrack.jetbrains.com/issue/IDEA-161626" rel="nofollow">Link</a>

Answer2:

Try changing

testCompile 'junit:junit:4.12'

to

androidTestCompile 'junit:junit:4.12'

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

Recommend

  • PDF document verify exception
  • Maven, JUnit, OpenEJB, ApplicationComposer leads to java.lang.NoSuchMethodError
  • Configure LESS compiler in IntelliJ IDEA Ultimate edition 14?
  • Is it possible to get the Cursive Plugin installed with Android Studio?
  • Intellij idea - how to always show method parameter names
  • Created a maven project using quickstart using intelliJ, didn't seem to generate any dir struct
  • Passing Query Parameters from HTML/JS App to Azure Server Script
  • Groovy install HTTPBuilder now that Codehaus shutdown?
  • general concept-java code and cycle clocks
  • Forcing a context switch from the userland on Linux?
  • jQuery-Marquee only working in Firefox
  • Rails importing CSV fails due to mal-formation
  • Lambda functions with FXML in JavaFX8
  • jQuery: click function bind in for-loop with closure fix
  • Flink: How to write DataSet to a variable instead of to a file
  • Regarding client side code generation from WSDL
  • Visual Studio not stopping on an exception being thrown
  • maven-dependency-plugin ignores outputDirectory configuration
  • Maven-Release-Plugin: Force to use specific version of scm provider
  • Spring Batch restart uncompleted jobs from the same execution and step
  • Where in the relevant specification is it documented that some comments in a SQL script are, in fact
  • How to package a jar and all dependencies within a new jar with maven
  • How do I configure Maven Cargo to use an embedded Tomcat server?
  • What is the equivalent of Android permissions in iOS development? [duplicate]
  • What is the best way to debug Bootstrap.groovy?
  • how to read a file in prolog?
  • What is this strange character in chrome's resource css viewer?
  • Thread synchronization with syncwarp
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation
  • Can't remove headers after they are sent
  • Redshift Querying: error xx000 disk full redshift
  • Bash if statement with multiple conditions
  • Suppressing passwd when calling sqlplus from shell script
  • How to remove a SwiftyJSON element?
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Swift: Switch statement fallthrough behavior
  • JQuery Internet Explorer and ajaxstop
  • Use of this Javascript
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#