Existing JUnit Class not found exception - java

I am trying to run a test from XSDGeneratorTest.java. But I get the following exception:
Class not found com.level3.pcat.xsd.XSDGeneratorTest
java.lang.ClassNotFoundException: com.level3.pcat.xsd.XSDGeneratorTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
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)
I tried to claen and build the project many a times but still same error!
I tried all the options from JUNIT Test class in Eclipse - java.lang.ClassNotFoundException, but none worked for me!

If that happens when you moved your project from one IDE/OS to another, just try to remove the JUnit3 library and add it again. Sometimes that fails when you bring your proyect from outside. I was stuck getting the same error until I did that.

Related

Type [unknown] not present Caused by: java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner

We are upgrading from junit-4.2 to junit-4.10. But with that, I am getting initializationError when launching firefox with below error stack. There is absolutely no reference to TestClassRunner (a class from junit-4.2) but not sure why we get it?
Type [unknown] not present
java.lang.TypeNotPresentException: Type [unknown] not present
at com.sun.proxy.$Proxy2.value(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
Caused by: java.lang.NoClassDefFoundError: org/junit/internal/runners/TestClassRunner
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:803)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:278)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3281)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3288)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3288)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3288)
at java.lang.Class.getAnnotation(Class.java:3229)
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Right click your project in Package Explorer > click Properties.
Go to Java Build Path > Libraries tab.
Click on 'Add Library' button.
Select JUnit.
Click Next.
OR
Run mvn clean compile test-compile
Tell me if it doesnt work, but it worked for me.
Well the issue was little difference. There were two classes of same name WebDriverRunner. The one that was getting loaded first (based on classpath entries) was incorrect (rather deprecated) one which had some reference of TestClassRunner class. After putting the other jar (with right WebDriverRunner) before the previous one resolved the issue. Thanks all for your suggestions.
I also faced this issue but not due to any code changes. In my case I was running a simple Robolectric test on the Android SDK 29 (as it was set in the TestConfiguration). I tried restarting Android Studio, cleaning the build and Invalidating cache but in the end this issue was resolved by changing the SDK to 28 in the Edit Configuration. I also don't have any solid reason to why this worked.

Jmockit code coverage and static initialization

We are developing unit test for a Java App that depends on an external jar. We use JMockit, and the unit test can run successfully. Then we enable JMockit code coverage. Then the Jmockit code coverage code somehow calls into the static initialization code of a class in the external jar. Since that class is not supposed to run in a test env, this causes an exception. Note that
1. The unit test can run successfully if code coverage is not enabled
2. The stack trace shows nothing of the class we are testing, it comes from the JMockit code (mockit.coverage.CodeCoverage.transform) to the static initialization of the third party class.
3. This is an external class in a jar, jmockit is not supposed to do code coverage about it according to the document, right? Does mockit.coverage.modification.ClassModification.modifyClassForCoverage mean instrumenting the code?
4. We tried -Djmockit-codecoverage-excludes flag, and it did not help.
Is this a bug of JMockit code coverage?
================ stack trace of error ============================
at THE_THIRD_PARTY_EXTERNAL_CLASS.<clinit>(THE_THIRD_PARTY_EXTERNAL_CLASS.java:line_number)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at mockit.internal.util.ClassLoad.loadClass(ClassLoad.java:102)
at mockit.internal.util.ClassLoad.loadClass(ClassLoad.java:54)
at mockit.internal.util.ClassLoad.loadByInternalName(ClassLoad.java:31)
at mockit.internal.util.ClassLoad.getSuperClass(ClassLoad.java:146)
at mockit.internal.util.ClassLoad.actualSuperClass(ClassLoad.java:177)
at mockit.internal.util.ClassLoad.whichIsSuperClass(ClassLoad.java:167)
at mockit.external.asm.ClassWriter.getCommonSuperClass(ClassWriter.java:1694)
at mockit.external.asm.ClassWriter.getMergedType(ClassWriter.java:1661)
at mockit.external.asm.Frame.merge(Frame.java:1426)
at mockit.external.asm.Frame.merge(Frame.java:1325)
at mockit.external.asm.MethodWriter.visitMaxs(MethodWriter.java:1475)
at mockit.external.asm.MethodVisitor.visitMaxs(MethodVisitor.java:804)
at mockit.external.asm.ClassReader.readCode(ClassReader.java:1554)
at mockit.external.asm.ClassReader.readMethod(ClassReader.java:1017)
at mockit.external.asm.ClassReader.accept(ClassReader.java:693)
at mockit.external.asm.ClassReader.accept(ClassReader.java:506)
at mockit.coverage.modification.ClassModification.modifyClassForCoverage(ClassModification.java:142)
at mockit.coverage.modification.ClassModification.modifyClassForCoverage(ClassModification.java:217)
at mockit.coverage.modification.ClassModification.modifyClass(ClassModification.java:192)
at mockit.coverage.CodeCoverage.transform(CodeCoverage.java:127)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:424)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getDeclaredMethods(Class.java:1808)
at org.junit.runners.model.TestClass.<init>(TestClass.java:38)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:65)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59)
at mockit.integration.junit4.JMockit.<init>(JMockit.java:30)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
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)

NoClassDefFoundError while setting up cucumber tests

I am getting the following error with my cucumber setup. I was following a youtube video (https://www.youtube.com/watch?v=pD4B839qfos&list=PL_noPv5wmuO_t6yYbPfjwhJFOOcio89tI) for this setup but I downloaded all the latest jars. Not sure what went wrong but now I can't figure out what needs to be done. Can anyone help me, please?
Exception in thread "main" java.lang.NoClassDefFoundError: gherkin/formatter/Formatter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at cucumber.runtime.formatter.PluginFactory$1.<init>(PluginFactory.java:53)
at cucumber.runtime.formatter.PluginFactory.<clinit>(PluginFactory.java:52)
at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:69)
at cucumber.api.cli.Main.run(Main.java:31)
at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: gherkin.formatter.Formatter
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 17 more
This means that Gherkin version you are using is not compatible with other Cucumber libraries. I tried using the latest gherkin3-3.0.0 but it did not work for me, so I degraded it to gherkin-2.12.2
I got below versions on Oct’17 for Cucumber
cobertura-2.1.1
cucumber-core-1.2.5
cucumber-java-1.2.5
cucumber-junit-1.2.5 cucumber-jvm-deps-1.0.5
cucumber-reporting-3.10.0 gherkin-2.12.2 junit-4.12
mockito-all-2.0.2-beta
The compatible version with your Cucumber-java can be found here under the Provided Dependencies section
If this does not resolve your issue make sure you remove jars from Project Build Path and add back again. Same if you are using maven then clearing local maven repository may also help.
You can also follow the steps mention in this tutorial

Exception running JUnit tests with AndroidStudio: java.lang.NoClassDefFoundError: android/os/Parcelable

I've created unit tests as per http://tools.android.com/tech-docs/unit-testing-support, but when I run them I get an exception:
java.lang.NoClassDefFoundError: android/os/Parcelable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getDeclaredMethods(Class.java:1860)
at org.junit.internal.MethodSorter.getDeclaredMethods(MethodSorter.java:54)
at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:74)
at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:11)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.ClassNotFoundException: android.os.Parcelable
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 33 more
Here is a snippet of one of my tests:
package com.aadhk.woinvoice.bean;
import com.aadhk.woinvoice.util.Constant;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class InvoiceCalculatorTest extends TestCase {
private static final double EPSILON = 1e-4;
public void testNoTaxNoDiscount() {
Invoice invoice = new Invoice();
List<InvoiceItem> items = new ArrayList<>();
InvoiceItem item = new InvoiceItem();
I'm using Android Studio 1.1, with Android Plugin Version 1.1.3 and Gradle version 2.2.1.
My code in question does implement the Parceable interface, but I was hoping there was a way to run these tests. The tests themselves do not exercise or use Parceable in any way.
Just ran into this using IntelliJ, but I'd imagine this would work for Android Studio as well.
I solved the issue by editing the run/debug configuration for my test and checking "Use alternative JRE" and trying a different platform of Android. I'd recommend trying out different ones and seeing if it resolves the issue.
As David Rawson said
Local Unit Tests run on your laptop or desktop in the JVM there. Hence, they do not have access to the native Android classes like Parcelable and if you run a local unit test against a class that uses those methods you will get errors.
So I suggest to put your test class in the path app/src/androidTest/java/your-project-package/your-test-class in your project directory. Also make sure to run your test on the device or emulator.

Java JSON Jar NoClassDefFound Caused by ClassNotFoundException

I have a simple java file packaged as a Jar on a server called by a PHP script. I have the "json-simple-1.1.1" library included in my java program. However, when I run it gives me the below error. I've added the json-simple jar to my project and import the correct classes. From what I've read it doesn't seem to be adding the json-simple library to my manifest file. Any ideas on how to fix this. I'm using eclipse if that helps.
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
at java.lang.Class.getMethod0(Class.java:2813)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Your library is probably not been loaded by classloader, so, if it is not in WEB-INF/lib, right click it and Add to build Path.

Categories