Run JUnit tests from a dependency jar in Eclipse

限于喜欢 提交于 2021-02-18 10:38:49

问题


I have some JUnit tests that contained in a .jar that is intended to be used as a library. The library contains some tests that should be run whenever the library is used in another project.

However when I create a new project using the library and run JUnit on it in Eclipse then the tests in the dependency .jar don't run / don't get detected by the JUnit test runner. I get the message:

No tests found with test runner 'JUnit 4'.

Is there a way I can configure the dependency .jar so that the tests will run alongside any tests that might be contained in the main project?

Basically I want the dependency .jar to "export" the tests to whatever projects it is used in.

I'm using Eclipse Juno, JUnit 4.10, and Maven for the dependency management.

EDIT:

The point of this library is to be able to help test projects that use it - i.e. it runs some specialised tests. This is why I want to be able to import the library .jar and have it contribute the extra tests to the importing project.


回答1:


I think that making a library of unit tests (@Test annotated methods) is a bad idea. However, making a library of reusable test components is a good one. We've done this in a few open source projects, and you can take a look how it works.

  1. One Maven module exports test components (we call them "mocks"), from src/mock/java directory. Exported artifact has -mock classifier. See rexsl/pom.xml (pay attention to highlighted lines).

  2. Mock artifacts are being deployed to Maven Central, together with usual artifacts: http://repo1.maven.org/maven2/com/rexsl/rexsl-core/0.3.8/ (pay attention to ...-mock.jar files)

  3. Modules that need that mocks can include them as usual artifacts, for example rexsl-core/pom.xml (see highlighted lines):

  4. Then, in your unit tests just use the classes from that mock libraries, like regular builders of mocks, for example: BulkHttpFeederTest

That's how you can make your test artifacts reusable, in an elegant way. Hope it helps.




回答2:


You can try Maven Surefire.

In some cases it would be useful to have a set of tests that run with various dependency configurations. One way to accomplish this would be to have a single project that contains the unit tests and generates a test jar. Several test configuration projects could then consume the unit tests and run them with different dependency sets. The problem is that there is no easy way to run tests in a dependency jar. The Surefire plugin should have a configuration to allow me to run all or a set of unit tests contained in a dependency jar.

This can be done as follows (Junit 3):

Ensure test jar contains a class which has a static suite() method

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {    
    public static Test suite() 
    { 
        TestSuite suite = new TestSuite( "All Tests"); 
        suite.addTestSuite(TestOne.class); 
        suite.addTestSuite(TestTwo.class); 
        return suite; 
    }    
}

Then in the project using the test-jar dependency: create a TestCase:

package org.melati.example.contacts;
import org.melati.poem.AllExportedTests;
import junit.framework.Test;
import junit.framework.TestCase;

public class PoemTest extends TestCase {    
    public static Test suite() 
    { 
        return AllExportedTests.suite(); 
    }
}

Now the tests will be found.




回答3:


@Mikera,

I find that this may help you. Just extend the Testcase Class to one of your java classes in project and you can run that particular class to run it as a JUnit Test.




回答4:


I am not sure that this is desirable - On the one hand, if you use a jar, its behaviour might be influenced by the external context, e.g. other libraries in the classpath. From inside the jar, there is no simple way to analyse this context and to adjust the tests accordingly. On the other hand, if you write and compile a library, you should test it before packaging it as a jar. You might even want to not include your tests.

If it is really important to you to run the tests again, I would be interested in what could make them fail without changing the jar. In that case, however, you might want to extend the testrunner. As far as I know it uses reflection. You can quite easily load jars in a classloader and go through all their classes. By reflection you can identify the test classes and assemble testsuites. You could look into the testrunner for an example. Still, you would need to start this process from outside, e.g. from inside one of your test classes in the client project. Here, QATest's approach might be helpful: By providing an overriden version of testsuite or testrunner, you could automate this - if the client uses your overridden API.

Let me know if this rather costly approach seems to be applicable in your scenario and I can provide code examples.




回答5:


Why should the user of the jar run the test cases inside the jar!!! When the jar is packaged and delivered, it means that the unit tests are run successfully. Typically, the jar itself should be either treated as a separate project or as one of the modules. In both the cases, unit test cases are run before its delivered.



来源:https://stackoverflow.com/questions/12175848/run-junit-tests-from-a-dependency-jar-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!