gmuecke / reflections

Automatically exported from code.google.com/p/reflections
Do What The F*ck You Want To Public License
0 stars 0 forks source link

Problems with unit tests via surefire #100

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

I have simple code based on reflections and it works fine. But I cannot run 
unit tests for this code via maven's surefire, surefire doesn't work with usual 
classpath 
(http://maven.apache.org/plugins/maven-surefire-plugin/examples/class-loading.ht
ml) and that's the reason I think.

Do you have any suggestion how to properly use reflections via surefire?

Thanks in advance.

Original issue reported on code.google.com by ig...@interlink-ua.com on 28 Jan 2012 at 11:37

GoogleCodeExporter commented 8 years ago
Indeed Surefire 2.9 changed the way classloading happens. With most setups the 
following should work (assuming the unit test class is called MyUnitTest):

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.addUrls(ClasspathHelper.forClass(MyUnitTest.class));
    // other configuration options here...
    Reflections r = new Reflections(cb);

Original comment by stephan...@gmail.com on 28 Jan 2012 at 3:49

GoogleCodeExporter commented 8 years ago
This bit me -- everything was running fine in the IDE, then I maven tested and 
bang.

Stephen's comment was very helpful; setting the URL's fixes it.  Thanks.

I discovered a few things that might be useful in constructing the set of 
URL's.  These both seem to pick up what I wanted (which was all my classes):

    ClasspathHelper.forPackage("com.mycompany")   // GOOD
    ClasspathHelper.forJavaClassPath()   // GOOD

Whereas this picks up the JARs/dirs being _tested_ but not dependencies:

    ClasspathHelper.forPackage("")   // MAYBE what you want

And this just picks up java (system) JARs and a 
target/surefire/surefirebooter####.jar:

    ClasspathHelper.forClassLoader(getClass().getClassLoader())   // BAD

HTH.

Original comment by alex.hen...@cloudsoftcorp.com on 24 Nov 2012 at 10:36

GoogleCodeExporter commented 8 years ago
any comments on that before this issue is closed?

Original comment by ronm...@gmail.com on 20 Feb 2013 at 9:25