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)
Related
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.
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.
The following strange error is returned. I cannot understand where it's coming from as the Netbeans debugger is quite lacking.
Exception in thread "AWT-EventQueue-0" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Address
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 modules.impl.EmailModule.process(EmailModule.java:41)
at agent.AgentDriver.callModules(AgentDriver.java:66)
at agent.AgentDriver.process(AgentDriver.java:33)
at agent.AgentUI.agentTextFieldActionPerformed(AgentUI.java:71)
Turns out this actually has to do with Java EE. This solution comes from here and here.
Java EE can screw up your classpath, so often it just needs to be removed and added again (or just removed). If your classpath has issues, you can occasionally get a java.lang.ClassFormatError, which is what happened here.
I am building an application using JBoss AS 7.1 and I am writing a test
#Test
public void testGetLoginUser() throws Exception {
final MarketCrudService crudService = new MarketCrudService(jpaRule.getEntityManager());
crudService.create(new Login("user1", "password"));
}
This test depends on #Rule created which instantiates a in-memory derby db for writing test data
When I run this test, I see following error
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/transaction/SystemException
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.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2248)
at org.jboss.logging.Logger.getMessageLogger(Logger.java:2214)
at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:141)
... snipped
This happens at line
emFactory = Persistence.createEntityManagerFactory(persistenceUnitName, persistenceProperties);
I googled and found out the this is because jta.jar is missing, so in my pom.xml I added
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
<version>1.0.1.Final</version>
</dependency>
Running again did not solved the problem, how can I fix it?
Adding this resolved the issue
<dependency>
<groupId>org.jboss.spec.javax.transaction</groupId>
<artifactId>jboss-transaction-api_1.1_spec</artifactId>
</dependency>
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.