antonsarov / powermock-osgi

Automatically exported from code.google.com/p/powermock-osgi
0 stars 0 forks source link

NoClassDefFoundError: org/hamcrest/Matchers #2

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I get an NoClassDefFoundError for org.hamcrest.Matchers, when i run my Test as 
OSGi PlugIn test, but when i run it as plain JUnit test everthing works as 
expected. I am using the OSGi version of PowerMock and have all neccessary 
dependencies in my launch config. What i am doing wrong? It seems like the 
Testrunner doesnt see the class, for some reason.

I created a reduced sample project and figured out that the Problem only appear 
when i use @PrepareForTest(XXX.class) in my class declaration.

See also: 
http://stackoverflow.com/questions/22456767/noclassdeffounderror-org-hamcrest-ma
tchers-using-powermock-osgi

Original issue reported on code.google.com by chriss....@gmail.com on 18 Mar 2014 at 7:37

GoogleCodeExporter commented 8 years ago

Original comment by chriss....@gmail.com on 18 Mar 2014 at 7:41

Attachments:

GoogleCodeExporter commented 8 years ago
>sample-project.zip 
contains the "test" project to reproduce the error and org.hamcrest.library 
modified for powermock

>PowerMock-Target.zip
contains all neccessary bundles to launch the "test" project, it can be used as 
target

Original comment by chriss....@gmail.com on 18 Mar 2014 at 7:44

GoogleCodeExporter commented 8 years ago
I tested a bit more, it seems to be a problem with org.hamcrest.library! 

@PrepareForTest(Final.class)
@RunWith(PowerMockRunner.class)
public class Test {

    @Test
    public void test() throws Exception {
        org.hamcrest.core.Is.is("");  //<- OK     bundle: org.hamcrest.core
        org.hamcrest.Matchers.is(""); //<- FAILS  bundle: org.hamcrest.library
    }
}

I doesn't work even when i add

   Eclipse-BuddyPolicy: global,app,ext,boot
   DynamicImport-Package: *

to the MANIFEST of org.hamcrest.library.

Original comment by chriss....@gmail.com on 18 Mar 2014 at 8:19

GoogleCodeExporter commented 8 years ago
I guess this is a problem with my "hacked" hamcrest bundle. Or it is actually 
the "non best practice" split up of hamcrest libraries. I have to export the 
org.hamcrest package in my hamcrest bundle as split package, otherwise the 
org.hamcrest package of the org.hamcrest.library will not get exported.

Please try the following, if you have time (or I will try it later):
- download the powermock-osgi sources
- update the 
https://code.google.com/p/powermock-osgi/source/browse/trunk/dependencies/hamcre
st-core/pom.xml to contain this:

<Export-Package>
  org.hamcrest;-split-package:=merge-last,
  org.hamcrest.core
</Export-Package>

- Build the powermock-osgi update site with the following two commands:
  ROOT>mvn clean install
  ROOT/p2>mvn clean install

- Delete the bundle pools in your eclipse
- Replace the URL with a local zip update site from the target folder of the 
p2/repository inside your target file
- try again

I can try it later. If you have success let me know, and I will rebuild a new 
update site.

Original comment by gabor.li...@gmail.com on 18 Mar 2014 at 10:03

GoogleCodeExporter commented 8 years ago
I tried something similar(?) in the MF of org.hamcrest.core:

i replaced -> Export-Package: 
org.hamcrest;library=split;version="1.3.0";mandatory:=library,
with       -> Export-Package: org.hamcrest;version="1.3.0",

Now it works!

Original comment by chriss....@gmail.com on 18 Mar 2014 at 10:44

GoogleCodeExporter commented 8 years ago
Super. I will update the update site as soon as I have time.

Original comment by gabor.li...@gmail.com on 18 Mar 2014 at 11:49

GoogleCodeExporter commented 8 years ago
I created a new update site at 
http://powermock-osgi.googlecode.com/svn/updateSite/1.5.4.1

Changes:
- Hamcrest declares org.hamcrest as split package
- Junit and mockito refers this "split package" part, and not the whole package.

I tried to make your test project working without success. Please try again 
with the new update site. Be sure to use my Junit and Mockito version.

Original comment by gabor.li...@gmail.com on 19 Mar 2014 at 11:43

GoogleCodeExporter commented 8 years ago
And define the org.hamcrest also split in your hamcrest library bundle

Original comment by gabor.li...@gmail.com on 19 Mar 2014 at 11:44

GoogleCodeExporter commented 8 years ago
See this comment as a real explanation for split packages. 
http://eclipsesource.com/blogs/2008/08/22/tip-split-packages-and-visibility/comm
ent-page-1/#comment-649

Earlier I could not understand what "library" and "core" stands for. Now it is 
clear. It is needed when someone want to depend on a split package part, for 
example the "core" part of org.hamcrest package.

Original comment by gabor.li...@gmail.com on 19 Mar 2014 at 11:48

GoogleCodeExporter commented 8 years ago
Fixed in 1.5.4.1 update site.

Original comment by gabor.li...@gmail.com on 21 Mar 2014 at 10:35