4

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

Ozgun Alan
  • 310
  • 3
  • 9
  • When do you get ClassNoFound errors? When running the tests as plain JUnit tests in Eclipse? – oberlies Apr 23 '13 at 13:48
  • Yes. From time to time, I encounter the problem when running rcp plugin tests as well. I could not define a consistent behavior yet. Besides, running and debugging sometimes yield different type of errors. I am a bit confused about which tool is governing what ? – Ozgun Alan Apr 23 '13 at 15:07

1 Answers1

2

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.

oberlies
  • 11,503
  • 4
  • 63
  • 110
  • Thanks for reply. There are no dependencies to OSGi. Just plain unit tests written by using Junit and Mockito. As I stated earlier I can not observe a certain pattern about run/debug junit behavior. Encountering source code not found at debug mode and sometimes class not found. Then NPE in run mode. However after some restart, clean, compile things may suddenly work. I suspect a collision between tycho, maven m2e, eclipse pde... Remark : test bundles are fragments. So aren't they in the same classpath of the target ? – Ozgun Alan Apr 26 '13 at 08:10
  • The collision is a good remark: the Tycho build and the PDE (if you imported with m2eclipse) compile to the same output folder. So if you call `mvn clean` and then run tests in Eclipse, there will be no classes and the tests fail. In this case, you need to trigger a rebuild in Eclipse by cleaning the project. – oberlies Apr 26 '13 at 11:23
  • Hi again. Actually I still have the problem & have noticed somebody had something similar to it. Explained here : http://stackoverflow.com/questions/16158355/junit-test-fails-with-java-lang-classnotfoundexception-com-mysql-jdbc-driver-on Interestingly my colleague using mac and I am using windows on virtualbox. In his case he can debug and see the source of NPE. In my case while try debugging I hit classNotFound exception so can not debug further. Similarly (to the discussion I linked at above) I encounter the error at the part of code where we use an external library that we bundled. – Ozgun Alan Apr 29 '13 at 10:16