views:

246

answers:

2

I use new Eclipse. Create demo test with jUnit (I added default jUnit library built-in Eclipse). Then I write this code:

import junit.framework.*;

import org.junit.Test;

public class SimpleTest extends TestCase { 
   public SimpleTest(String name) { 
      super(name);
   }
   public final void main(String method){

   }

   @Test
   public final void testSimpleTest() {
      int answer = 2;
      assertEquals((1+1), answer); 
   }
}

But it doesn't run. In the Debug tab:

org.eclipse.jdt.internal.junit.runner.RemoteTestRunner at localhost:52754 
Thread [main] (Suspended (exception ClassNotFoundException)) 
URLClassLoader$1.run() line: not available [local variables unavailable] 
AccessController.doPrivileged(PrivilegedExceptionAction<T>, AccessControlContext) line: not available [native method] 
Launcher$AppClassLoader(URLClassLoader).findClass(String) line: not available 
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available 
Launcher$AppClassLoader.loadClass(String, boolean) line: not available 
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available 

How can I solve this?

+1  A: 

Remove the breakpoints on Exceptions when running in debug mode, or just run in non-debug mode.

In the debug view, on the right top box click the Breakpoints tab and uncheck any breakpoint on an Exception, e.g. ClassNotFoundException and rerun the test.

BalusC
I have no breakpoint. And when I run in non-debug mode, it says Could not find the main class: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. Program will exit.
KimKha
+1  A: 

You, as many people, have confused the JUnit 3 and JUnit 4. If you are using JUnit 3 name your tests "test*" and inherit from TestCase. If you are using JUnit 4 use annotations.

Gabriel Ščerbák
While this is true, this is not the *root cause* of this problem. Copypaste yourself, put a breakpoint on `ClassNotFoundException` and run in debug. You see, exactly the same problem until breakpoint is removed.
BalusC
Nice to know the difference between the two versions.
James P.
How to do that?I write new code:import static org.junit.Assert.*;import org.junit.Test;public class SimpleTest { @Test public final void abcTest() { int answer = 2; assertEquals((1+1), answer); }}But it's still error
KimKha
@KimKha that should run properly (tried it), can you give the exception? Make sure you have JUnit 4 in dependencies.
Gabriel Ščerbák
@KimKha I also tried your original example and it works (although it is a v3 and v4 mix). I tried fixing it (removed TestCase supr class and super constructor call) and I only got problem with having a constructor, but after deletion worked like a charm. Another thing which comes to my mind is if you are running it like a JUnit Test or Java application. In Eclipse you should run the test using Alt+Shift+X,T .
Gabriel Ščerbák
I used default jUnit4 in Eclipse. When I press Alt+Shift+X,T. It fired error: Could not find the main class: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner. Program will exit
KimKha
@KimKha seems like something with classpath/classloader, definitely not my thing...
Gabriel Ščerbák