ClassNotFoundException: junit.framework.TestCase cannot be found by org.eclipse.xtext.junit_2.4.3.v201309030823
Asked Answered
S

4

16

I'm puzzled by this error:

java.lang.NoClassDefFoundError: junit/framework/TestCase
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:188)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClassHoldingLock(ClasspathManager.java:638)
...
    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.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62)
    at org.eclipse.pde.internal.junit.runtime.CoreTestApplication.run(CoreTestApplication.java:23)
...
Caused by: java.lang.ClassNotFoundException: junit.framework.TestCase cannot be found by org.eclipse.xtext.junit_2.4.3.v201309030823
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 78 more

The exception happens when I run the tests for my Xtext DSL as plugin tests (i.e. when a new Eclipse platform is started internally). This happens before any of my code is executed.

When I look at the plugin dependencies, I can see that org.junit_4.11.0.v201303080030.jar is on the classpath and I also see /.../workspace/.metadata/.plugins/org.eclipse.pde.core/.external_libraries/org.junit_4.11.0.v201303080030/junit.jar

So OSGis should definitely be able to resolve this dependency.

Does Xtext 2.4.3 work with JUnit 4.11 or do I have to downgrade my Eclipse install to JUnit 4.10?

Static answered 16/1, 2014 at 15:27 Comment(2)
when does this happen? And also are you on Kepler?Gunilla
I've downloaded Eclipse Kelper JEE SR1 (4.3.1). My target platform was built using the Xtext 2.4.3 archived update site and MWE2 2.4.1.Static
U
8

Two points

First

Check your Project Properties->Java Build Path->Libraries (tab). JUnit should be there although this usually will show up in the build.

Check you Project's Run Configurations->JUnit->Classpath (tab). JUnit should be under User Entries for your project.

Second

Check you have below plugins

org.eclipse.xtext.xbase.junit
org.junit (3.8.2)
org.junit (4.8.2)

[EDIT] This got me on the right track. To fix the error make sure you have org.junit 3.8 in the target platform!

Explanation: The error above means that Eclipse itself couldn't start. It seems that the JUnit runner has a dependency on JUnit 3.8. It won't be used but without it, the whole platform can't be initialized.

You can see whether you have the same problem by looking at the stack trace: Does it contain RemotePluginTestRunner.main?

To fix the error, add the org.junit 3.8 bundle to your target platform.

Understrapper answered 24/1, 2014 at 13:37 Comment(3)
I'm using JUnit 4. The libraries tab lists two JUnit JARs. Once org.junit_4.11.0.*.jar from my target platform and once workspace/.metadata/.plugins/org.eclipse.pde.core/.external_libraries/org.junit_4.11.0.v201303080030/junit.jar. I think the latter is added my the JUnit Plugin Test launcher.Static
Plugins Tests don't have a Classpath tab in Run Configurations. But I checked the Plug-ins tab. In there, I only saw org.eclipse.jdt.junit.runtime_3.4.400.v20130514-0733.jar from my target platform. I manually added org.eclipse.jdt.junit4.runtime_1.1.300.v20130514-0733.jar but the error is still the same :-(Static
You were correct. Despite the fact that org.eclipse.xtext.junit4 imports org.junit 4.5.0, org.eclipse.xtext.junit (note the missing "4" at the end) seems to have a dependency to JUnit 3.8. After adding the old, outdated JUnit bundle, the plugin tests started.Static
A
2

Create a class with below code in your project-

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class TestJunitLib {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void test() {
    fail("Not yet implemented");
}

}

On saving if eclipse shows compiler errors. Press Ctrl + 1 ( Quick Fix ) and select one of the below-

a) Add jUnit 4 library to buildpath b) Fix project setup

If a does not appear your eclipse installation is missing jUnit 4 plugin. In that case google up on how to install jUnit in eclipse.

Aborticide answered 30/1, 2014 at 8:9 Comment(2)
this works. My problem is that Eclipse itself wasn't starting when the org.junit 3.8 bundle wasn't in the target platform.Static
I'm trying to run an Eclipse JUnit Plugin test. See my edits to the accepted answer.Static
R
1

You may have have unresolved issues in your pom.xml file. Open the problems view solve accordingly. Then ideally you will be able to run the test cases successfully without encountering the classnotfoundexception.

Raymer answered 17/2, 2015 at 12:48 Comment(1)
As I explained in the accepted answer, this isn't about Maven. The error happens when Eclipse itself is started. hence there is nothing in the problems view.Static
M
0

Two possible places to check for more details:

  1. Decompile the jar you mentioned (jar tvf or javap -classpath and specify the JAR), and check for its dependecies
  2. Check eclipse.ini for some initialization variables that could possibly be fixed.
Mcintyre answered 24/1, 2014 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.