Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 318299

Summary: JUnit can not find test class if you run with Categories runner
Product: [Tools] Orbit Reporter: Adomas <adomas.greicius>
Component: bundlesAssignee: David Williams <david_williams>
Status: RESOLVED NOT_ECLIPSE QA Contact:
Severity: normal    
Priority: P3 CC: adomas.greicius, alf_s, david, david_williams, jan.sievers, markus.kell.r, niklas.deutschmann, saulius.tvarijonas, simon_kaegi
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on:    
Bug Blocks: 386964    

Description Adomas CLA 2010-06-29 08:03:31 EDT
Build Identifier: 20100617-1415

Junit Categories class tries to load test class and get ClassNotFoundException.
In Junit 4.8.X manifest should be added Eclipse-BuddyPolicy: dependent



Reproducible: Always

Steps to Reproduce:
1.Create suite case with category
2.Run suite case
Comment 1 Simon Kaegi CLA 2010-06-29 09:15:43 EDT
Orbit is meant to hold 3rd party libraries packaged as OSGi bundles in a fairly standard way emulating how the 3rd party library owner might package it. Eclipse-BuddyPolicy is not a standard OSGi header so I think we probably should not add this header.

Instead you can provide the visibility you need by creating an OSGi fragment of the JUnit bundle that imports your category class.
Comment 2 Adomas CLA 2010-06-29 09:39:55 EDT
Category is standard junit  4.8.1 feature. 
http://kentbeck.github.com/junit/doc/ReleaseNotes4.8.html
Main problem is in Junit org.junit.runner.Description

	public Class<?> getTestClass() {
		String name= getClassName();
		if (name == null)
			return null;
		try {
			return Class.forName(name);
		} catch (ClassNotFoundException e) {
			return null;
		}
	}

getTestClass could not load classes from other bundles.

I can change my code in many ways, but Junit runner in eclipse do not run tests without this policy. Main problem is that Category runner could not load Junit  test class not category class. 
There is Bundle-BuddyPolicy, but I am not sure if it works.
Comment 3 Adomas CLA 2010-10-20 12:12:52 EDT
DynamicImport-Package: * would solve this problem
Comment 4 Saulius Tvarijonas CLA 2010-10-21 02:36:57 EDT
Simon,

I agree to Adomas on this issue. JUnit library CATEGORY feature depends on it, so it should a standard OSGi header for JUnit. Actually it doesn't matter how we will achieve it, using BuddyPolicy or DynamicImport. The main idea is if you will try use categories feature from JUnit you will fail with current bundle manifest.

IMO making a fragment or changing manifest for everyone is not a convenient option. What is the point of such a standard if it blocks some features? Moreover everybody who will want to use this JUnit feature, will need to waste a time on figuring out why it doesn't work in Eclipse. Why not make everybody happy? :)

Simon, what problems do you see here if we change BuddyPolicy or DynamicImport?
Comment 5 DJ Houghton CLA 2010-11-18 14:16:37 EST
Markus, do you have thoughts on this one?
Comment 6 Alfred Schmid CLA 2010-11-19 09:44:58 EST
Exists a workaround for this problem now, I would be very happy if a solution would be provided soon.
Comment 7 Markus Keller CLA 2010-11-19 11:24:43 EST
I first also thought we have no other choice than opening up the visibility, but after a closer look, I found that this is a design bug in recent versions of JUnit 4.

From the beginning, the framework has been designed carefully to avoid classloader issues: It passes around Class objects and avoids reflection to find classes. org.junit.runner.Description#getTestClass() was a bad idea, since it breaks this design.

Please follow up with the JUnit team. This bug should be fixed in JUnit itself (ideally before Categories gets moved out of the "experimental" package).
Comment 8 David Saff CLA 2010-11-19 11:38:36 EST
(In reply to comment #7)
> I first also thought we have no other choice than opening up the visibility,
> but after a closer look, I found that this is a design bug in recent versions
> of JUnit 4.
> 
> From the beginning, the framework has been designed carefully to avoid
> classloader issues: It passes around Class objects and avoids reflection to
> find classes. org.junit.runner.Description#getTestClass() was a bad idea, since
> it breaks this design.
> 
> Please follow up with the JUnit team. This bug should be fixed in JUnit itself
> (ideally before Categories gets moved out of the "experimental" package).

org.junit.runner.Description#getTestClass() was a bad idea for so many reasons.  The current version of Categories will not make it out of experimental.
Comment 9 Niklas Deutschmann CLA 2011-09-19 10:47:03 EDT
I found the following workaround: 

Make JUnit and your test plugins class-loading buddies (add "Eclipse-BuddyPolicy:registered" to JUnit's MANIFEST.MF in your target platform and "Eclipse-RegisterBuddy: org.junit" to the MANIFEST.MFs of your test plugins)
Comment 10 David Williams CLA 2013-03-03 16:33:33 EST
If I'm reading the chain of comments correctly, the fundamental problem was not in our bundle definition, but JUnit code. And we typically don't "fix" (or work around) other code that's being actively developed. 

I hope its been fixed in more recent versions? If not, maybe consumers should avoid that experimental class?

But in any case, will close this bugzilla was as "not eclipse". 

Please re-open if I've misunderstood.
Comment 11 Jan Sievers CLA 2013-03-04 04:23:45 EST
just for reference, here is the junit bug causing the issue
https://github.com/junit-team/junit/issues/364