问题
I joined a project where the development is carried on Eclipse RCP platform with the use of Equinox/OSGi, Tycho.
I am slowly getting familiar with classpath/classloading mechanism of OSGi, how tycho, equinox works etc.
I am writing plain unit tests with the use of mocking by collecting them in test bundles (which are fragments of the target bundles) however I am encountering ClassNotFound errors, issues with attaching source code. I have seen recommendations such as running tests as rcp plugin tests or with tycho at integration-test scope. However I found all those approaches very slow for running tests and counter-productive.
As far as I understood, somehow dependency resolution mechanism in this environment (Tycho, Eclipse RCP or both) is a bit different than my assumptions. When I add new implementations to the host bundle to satisfy my tests, all the new implementations are not picked up by the test bundle unless I run mvn install (tycho is taking over) for the host bundle. Sometimes that requires full installation of the project because of missing dependencies. Another method I tried was to run tests as JUnit-plugin test. That method is quite slow as well due to dependency resolution of tycho and it is loading all bundles.
I would like to know what is the best approach for running unit tests ? (when I say unit tests I mean the true definition of unit tests where interactions are mocked and tests run in miliseconds).
回答1:
As long as your tests don't depend on OSGi, you can execute them as with Run as > JUnit Test in Eclipse. This will launch the test without an OSGi runtime, but with a normal class path and all bundles treated as normal JARs. This works as long as the test and code under test don't require anything from the OSGi runtime like bundle activation, services, etc.
In case there is such a dependency, the test will often fail due to uninitialized fields, e.g. with NPEs. In this case, you'll need to run the tests as JUnit Plug-in test in Eclipse. You can tweak the start-up time of these tests
- by changing the Program to Run to Run an application: [No Application] - Headless Mode, and
- by selecting only those bundles that are really needed to run the test. This can be quite complicated to get right, so I wouldn't recommend this if you are new to OSGi.
Running the tests as part of a full Maven/Tycho build is usually a lot slower than running the tests in Eclipse. Personally, I only do this as output qualification before pushing the change into the central repository.
来源:https://stackoverflow.com/questions/16166866/how-to-follow-test-first-approach-with-equinox-osgi-tycho-eclipse-rcp-enviro