简体   繁体   中英

How can I test a service provider implementation module with Junit 5?

This is my base module which needs implementations of interfaces defined in myspi package. Various providers can offer MyProvider implementations. Base module uses them via myspi.MyProvider interface implementation.

module base {
    exports myspi;
    uses myspi.MyProvider;
}

This is my sample implementation module which provides the MyProvider implementation with MyProviderImpl

module myspi.provider {
    provides myspi.MyProvider with myspi.provider.MyProviderImpl;
}

All these work fine when I load the implementations in base module, with

public static List<MyProvider> getMyProviders() {
        var myProviders = new ArrayList<MyProvider>();
        for (MyProvider myProvider : ServiceLoader.<MyProvider>load(MyProvider.class)) {
            myProviders.add(myProvider);
        }
        return myProviders;
    }

But same code returns empty list in Junit 5 test code (ServiceLoader returns null). How can I test the service provider modules with Junit 5. Or is there any alternative to Junit that allows us to create test modules (modularized test API) that declares "uses myspi.MyProvider" in the module-info and works fine with getMyProviders()?

Basically you're on the right track. You need to convince the Java module system that your test modules are the single source of thruth when it comes to resolve modules are test runtime.

Black-box testing is easy.

White-box testing in the modular world, meaning testing protected and package private members within a module, is tricky. There are at least two ways to achieve this: a) use java command line options to configure the Java module system at test startup or b) blend main sources into the test sources at compile time and maintain a dedicated module-info.java in your test sources.

Please visit the links to the blogs and examples posted over at How to make a modular build with jdk > 1.8 Here is an excerpt for convenience:

Examples

Background and other resources

And expect most IDE to not support you either. For now.

SOLVED!

I've removed the Junit from class-path to module-path and also removed all Junit 4 compatibility stuff such as RunWith() etc, and made my test pure Junit 5 test.

I've added a module-info.java (Junit 5 doesn't require an open module although the books tell the opposite)

After I've modularized the tests I found that it still doesn't execute the ServiceLoader stuff. Then I've started looking for the fault myself.

And I found it! Running the ServiceLoader stuff in base module was possible, because the base module refers to the exported myProvider.jar, which in turns access a myProvider-config.properties file in the same directory. Without this config file myProvider cannot work properly.

The problematic test module on the other hand, refered the eclipse project of the myProvider instead of its exported .jar file and hence could not find its config file and exits. I'd moved this config file from Netbeans to Eclipse simply copying it into the same directory. Thus missing config file was the problem.

Changing the project settings I could run the tests without any failure.

I would like to thank all the contributors who responded.

这是一篇很老的帖子,但如果有人来到这里,gradle mudules 插件会处理一切: https : //github.com/java9-modularity/gradle-modules-plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM