Alex Rivera | Logout

Run JUnit tests from a dependency jar in Eclipse

Asked 2012-08-29T10:26:21.983
11

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.

Edit
Report

2 Answers

2

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.

answered 2012-09-10T15:16:16.280
1

@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.

answered 2012-09-04T04:57:37.510

Your Answer