title: Testing ServiceLoader in Eclipse Plugins
body: <p>I have four Eclipse plugin projects (create a new Java Project, right-click, configure, Convert to Plugin Project) in my workspace. The first (<code>my.runtime</code>) contains an interface (<code>MyFactoryInterface</code>) and a class (<code>MyClient</code>) that defines a method (<code>List&lt;String&gt; getAllFactoryNames()</code>) that loads all implementations of that interface via <code>java.util.ServiceLoader</code> and calls a method (<code>String getName()</code>) on them, collecting the results and returning them in a list.</p>
  
  <p>To test that class, I have a JUnit test in the second project (<code>my.runtime.test</code>, set up with <code>my.runtime</code> as Fragment-Host), checking if the name returned by a dummy implementation(<code>MyDummy</code>, returning <code>"Dummy"</code>) I have in the <code>my.runtime.test</code> project is in the list returned by <code>MyClient.getAllFactoryNames()</code>. So far, it works fine.</p>
  
  <p>In the third project (<code>my.extension</code>, with <code>my.runtime</code> as dependency) I have a class (<code>MyHello</code>) that uses the names returned by <code>MyClient.getAllFactoryNames()</code> to return a list of greetings (<code>"Hello "+name</code>).</p>
  
  <p>Again, to test this, I have a project (<code>my.extension.test</code>, with <code>my.extension</code> as Fragment-Host) containing another Implementation (<code>MyWorld</code>, returning <code>"World"</code> as name) and a JUnit test case checking if <code>"Hello World"</code> is in the greetings returned by <code>MyHello.getGreetings()</code>. This test fails, as MyClient still only finds the <code>MyDummy</code> implementation, and not the <code>MyWorld</code> implementation. Both implementations are accompanied by matching entries in <code>META-INF/services/my.runtime.MyFactoryInterface</code> files.</p>
  
  <p>I currently use the following code to load the implementations:</p>
  
  <pre><code>ServiceLoader&lt;MyFactoryInterface&gt; myFactoryLoader = ServiceLoader.load(MyFactoryInterface.class);
      for (MyFactoryInterface myFactory : myFactoryLoader) {
  </code></pre>
  
  <p>I know that I can supply a ClassLoader as a second argument to <code>ServiceLoader.load</code>, but I have no idea how to get one that knows all plugin projects... any suggestions? Or is ServiceLoader not the right tool for this problem?</p>