Bug 155438 - [JUnit] TestSuite suite() does not work with Junit 4
Summary: [JUnit] TestSuite suite() does not work with Junit 4
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: needinfo
Depends on:
Blocks:
 
Reported: 2006-08-28 14:04 EDT by Kasper Nielsen CLA
Modified: 2009-08-30 02:06 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kasper Nielsen CLA 2006-08-28 14:04:50 EDT
The suite() method is not picked up by projects using junit 4.0

Steps to reproduce:

1.
Create a Java Project and add Libraries->Add Library->Junit 4

2. 
Add the following 2 classes

--RunMe.java
import junit.framework.Test;
import junit.framework.TestSuite;

public class RunMe {
	public static Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTestSuite(MyTest.class);
		return suite;
	}
}


--MyTest
import junit.framework.TestCase;

public class MyTest extends TestCase {
	public void testFoo() {
		System.out.println("foo");
	}
}

3.
Try and run test RunMe, fails with following exception
java.lang.Exception: No runnable methods
	at org.junit.internal.runners.TestClassMethodsRunner.testAborted(TestClassMethodsRunner.java:42)
	at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:33)
	at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
	at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
	at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


This works fine, if junit 3.8.1 is selected instead of junit 4.0.
Furthermore, even if I start out by choosing 4.0 and then try to switch to 3.8.1 in project properties I get this error when trying to run RunMe

Cannot find 'org.junit.Test' on project build path. JUnit 4 tests can only be run if JUnit 4 is on the build path
Comment 1 Olivier Thomann CLA 2006-08-28 14:07:47 EDT
Moving to JDT/UI
Comment 2 Martin Aeschlimann CLA 2006-08-29 11:56:32 EDT
JUnit 4 eliminated 'suite': It is not supported anymore. Have a look at JUnit 4 documentation on the net how multiple tests are executed now.
Comment 3 Kasper Nielsen CLA 2006-08-29 17:25:44 EDT
That's not correct. JUnit 4 didn't eliminate suite() it just provided a new alternative the Suite annotation. JUnit 4 is fully backwards compatible with JUnit 3.8.x. 
Comment 4 Martin Aeschlimann CLA 2006-08-31 12:35:03 EDT
So how exactly do you use JUnit 4 so that you can run 'RunMe' as listed in comment 0?

AFAIK, you can still have a static method suite, but you will have to annotate your type with
@RunWith(AllTests.class)

Unfortunatly that doesn't work at the moment due to a bug in JUnit (NPE in Descriptor.hashCode). So you have to name your test suite
@RunWith(AllTests.class)
public class RunMe {
        public static Test suite() {
                TestSuite suite = new TestSuite();
                suite.setName("MySuite"); 
                suite.addTestSuite(MyTest.class);
                return suite;
        }
}
Comment 5 Kasper Nielsen CLA 2006-09-01 02:01:36 EDT
(In reply to comment #4)
> So how exactly do you use JUnit 4 so that you can run 'RunMe' as listed in
> comment 0?

How I use JUnit 4? You mean like Run->Run As->JUnit Test ?

The option to run them as JUnit Test is still shown for JUnit 4 projects if a static suite() method is available.
Comment 6 Martin Aeschlimann CLA 2006-09-01 03:20:23 EDT
As far I know and just verified, JUnit 4 can not run the class as specified in comment 4. For example JUnitCore.main("pack.RunMe") does not find any tests. I was wondering if you know more about the JUnit4 framework and I missed a way to do it. Yes, I also heard that Junit 4 is 'backward compatible' but it doesn't seem to be in that aspect.

David, is that a JUnit bug?
Comment 7 Martin Aeschlimann CLA 2006-09-19 11:47:17 EDT
So I assume this is a JUnit 4 shortcoming. Setting to remind unless I hear something else.
Comment 8 David Saff CLA 2006-09-26 15:16:32 EDT
Martin,

I've been unable to do much with JUnit over the summer, so I haven't had a lot of time to think about this until now.  It appears that Eclipse 3.2 M2 does correctly run the code indicated in comment 4.  Is this new behavior?

Thank you for noting the NPE bug--we'll fix that in the next release of JUnit.
Comment 9 Martin Aeschlimann CLA 2006-09-27 03:17:53 EDT
The code from comment 4 always workes in Eclipse, but it does'n run when you leave the line 'suite.setName("MySuite")' away: Then you get the NPE.
With 'shortcoming' I meant the fact that '@RunWith(AllTests.class)' is required here. You can't just run your old class with suite anymore. So JUnit 4 isn't exactly backwards compatible. So I'm not sure if this is a JUnit 4 oversight or by design.
Comment 10 Denis Roy CLA 2009-08-30 02:06:48 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.