Java’s ServiceLoader needs those entries to be present inside the JAR file. Is there a way to programatically add those service entries at runtime time for unit testing, when inside the IDE? Especially when the JARs are not built yet.
Java’s ServiceLoader needs those entries to be present inside the JAR file. Is there
Share
Don’t get too hung up focusing on JAR files. They are the preferred way to encapsulate services, but they aren’t required. The key is really
ClassLoader.getResources(String)– where theStringarg effectively becomes("META-INF/services/" + serviceClass.getName()). One other bit of information to keep in mind is thatServiceLoader.load(Class)makes use of the context class loader (of course, you can also make use ofServiceLoader.load(Class, ClassLoader)). So…what you really need to do is manipulate the classpath or configure the context class loader in such a way as to makeServiceLoaderhappy.