Adding test-only dependencies for non-OSGi JUnit test in a Tycho project

江枫思渺然 提交于 2019-12-06 07:15:31

问题


I have several OSGi-bundles that I build with Eclipse Tycho. All code dependencies are managed manifest-first.

Now I want to develop some pure JUnit-Tests in order to test bundle-internal functionality. These tests do not need an OSGi-container in order to be executed, but they do need Mockito.

As Mockito obviously is only needed when this non-OSGi-tests are being run and not during the execution of the bundle itself, it feels wrong to add the dependency to the MANIFEST.MF file.

My current approach is that I added a project/lib-folder that holds the mockito.jar and that I added this jar to his classpath manually. This works for local eclipse execution, which is fine for the moment.

The problem is, that every coworker needs to add the jar to the classpath as well, as the .classpath-file is obviously not checked in. Also, I guess that there will be problems when the tests eventually will be executed e.g. on a build server because of the missing classpath entry.

So my question is: How can I add the dependency to Mockito in a way that will work effortless for every coworker and will not cause any problems during bundle execution?

I could add it as optional dependency to the MANIFEST.MF-file, but as stated above, it doesn't feel like thats the correct solution.

Can I add the mockito-dependency as normal pom-first dependency with scope test or will this cause conflicts with the normal manifest-first approach?

I also found the maven-eclipse-plugin which offers a classpathContainers configuration option, but I did not find a similar option to add a library to the classpath.

The tests are located in the src/test/java folder of the bundle itself.

What is the best way to add test-only dependencies to a project built with eclipse Tycho, given that I technically don't need Tycho in order to execute those tests?


回答1:


If you can run your test with a class path that you completely only compose of Maven dependencies (e.g. to Mockito), you could also create a module with jar packaging for these tests. From that module, you'd also add a dependency to the bundle, which would then be used as plain JAR.

What you'd loose in this case is the resolution of transitive dependencies. The jar project would then be resolved by Maven, and Maven doesn't know about the Manifest dependencies of the OSGi bundle. But depending on the exact nature of what you want to test, this may still work.




回答2:


Typically you'll see tests in a separate bundle, with it's own manifest and dependencies. This is not the standard Maven structure, but it fits better with how OSGi bundles want to be structured.

For example, look at the JDT-core git repository, you'll see separate test bundles. Note that test bundles have a special packaging-type of eclipse-test-plugin.



来源:https://stackoverflow.com/questions/33545438/adding-test-only-dependencies-for-non-osgi-junit-test-in-a-tycho-project

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