0

I am using ServiceLoader to dynamically load Plugins. In purpose of unit testing I want to Mock some behavior of these Plugins. These Mock-Plugins should be used when unit testing, but not when building the Project. How would one do this though?

I feel like this question is IDE / build tool specific, for that matter I am using Eclipse.

Project Structure:

  • src
    • app
      • app.java
    • scraper
      • ScraperController.java
      • Scraper.java
  • Scrapers
  • tests
    • mockups
      • FailingScraperMock.jar
      • ...
    • unit_tests
      • ServiceLoaderWithProvidedMockScrapersTest.java
      • ...

scraper.Scraper.java is the provider interface

Scrapers is where all the Scraper-Providers go

I tried to exclude the Mock-Plugins for the source folder: src/ . Either I am doing it wrong or that's not intended for what I am trying to do.

Would appreciate any kind of help / information.

1 Answers1

0

If you mark your test folder (/tests) as test code and then it should be excluded from application builds.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
  • Test code is already not being build. My problem is that I want plugins specific only for tests and build specific plugins. But when adding an test only plugin its also used in the actual production code which I dont want – ClusterFonk Mar 06 '21 at 22:13
  • I don't get it. You state that you marked your test folder as test code and that is does not get built in production builds. At the same time you state that the mock-plugins do get used in the production code. How is that possible if they do not even get built? – EricSchaefer Mar 07 '21 at 09:15
  • The plugins are distributed as jar files. Loaded **dynamically** by the ServiceLoader, they are not static. Normally one adds a directory to the classpath where all the jars go and the serviceloader is able to load them. But doing so exposes these plugins to test AND production code. – ClusterFonk Mar 07 '21 at 12:47
  • Could you have two jars for the plugins? One for the real plugins and one for the mocks? In that case you can treat them differently for test builds and production builds. – EricSchaefer Mar 07 '21 at 15:06
  • yes, mocks are different jars, but my problem is how to tell the build tool, in my case eclipse, to treat them differently. – ClusterFonk Mar 08 '21 at 00:02