I have a method that uses ServiceLoader
to load services using resources.
public List<String> getContextData(int Id)
{
List<String> list = new ArrayList<String>();
ServiceLoader<ContextPlugin> serviceLoader = ServiceLoader.load(ContextPlugin.class);
for (Iterator<ContextPlugin> iterator = serviceLoader.iterator(); iterator.hasNext();)
{
list .addAll(iterator.next().getContextData(Id));
}
return list;
}
How should I unit test above method using Junit?
You need to copy the "provider-configuration file" into your test class directory.
assuming your test class files are located at
test/classes/
you need to copy the "provider-configuration file" to
test/classes/META-INF/services/your.package.ContextPlugin
How to copy the files depend on your build tool (e.g. maven, gradle, ant, ...)
As example for maven you should store them in the test resources folder.
src/test/resources/META-INF/services/your.package.ContextPlugin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With