如何使用Junit 5测试服务提供商实施模块?

时间:2021-09-18 20:58:22

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.

这是我的基础模块,需要在myspi包中定义接口的实现。各种提供商可以提供MyProvider实现。基础模块通过myspi.MyProvider接口实现它们。

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

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

这是我的示例实现模块,它为MyProvider实现提供了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()?

但是同样的代码在Junit 5测试代码中返回空列表(ServiceLoader返回null)。我如何使用Junit 5测试服务提供者模块。或者是否有任何替代Junit允许我们创建测试模块(模块化测试API),在模块信息中声明“使用myspi.MyProvider”并且与getMyProviders一起工作正常( )?

2 个解决方案

#1


1  

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.

基本上你是在正确的轨道上。在解析模块是测试运行时时,您需要说服Java模块系统您的测试模块是唯一的thruth源。

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.

模块化世界中的白盒测试,意味着在模块中测试受保护和封装私有成员,是棘手的。至少有两种方法可以实现这一点:a)使用java命令行选项在测试启动时配置Java模块系统或b)在编译时将主源混合到测试源中并在您的应用程序中维护专用的module-info.java测试来源。

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:

请访问在如何使用jdk进行模块化构建时发布的博客和示例的链接> 1.8以下是为方便起见的摘录:

Examples

Background and other resources

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

并期望大多数IDE也不支持你。目前。

#2


1  

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.

我已将Junit从class-path移除到module-path,并删除了所有Junit 4兼容性内容,如RunWith()等,并使我的测试纯粹是Junit 5测试。

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

我添加了一个module-info.java(Junit 5不需要一个开放的模块,尽管这些书反过来说明了)

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.

在我模块化测试后,我发现它仍然没有执行ServiceLoader的东西。然后我开始自己寻找故障。

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.

我找到了!可以在基本模块中运行ServiceLoader内容,因为基本模块引用导出的myProvider.jar,后者又访问同一目录中的myProvider-config.properties文件。如果没有此配置文件,myProvider将无法正常工作。

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.

另一方面,有问题的测试模块引用了myProvider的eclipse项目而不是其导出的.jar文件,因此无法找到其配置文件并退出。我将此配置文件从Netbeans移动到Eclipse,只需将其复制到同一目录中即可。因此缺少配置文件是问题所在。

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

更改项目设置我可以毫无失败地运行测试。

I would like to thank all the contributors who responded.

我要感谢所有回复的贡献者。

#1


1  

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.

基本上你是在正确的轨道上。在解析模块是测试运行时时,您需要说服Java模块系统您的测试模块是唯一的thruth源。

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.

模块化世界中的白盒测试,意味着在模块中测试受保护和封装私有成员,是棘手的。至少有两种方法可以实现这一点:a)使用java命令行选项在测试启动时配置Java模块系统或b)在编译时将主源混合到测试源中并在您的应用程序中维护专用的module-info.java测试来源。

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:

请访问在如何使用jdk进行模块化构建时发布的博客和示例的链接> 1.8以下是为方便起见的摘录:

Examples

Background and other resources

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

并期望大多数IDE也不支持你。目前。

#2


1  

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.

我已将Junit从class-path移除到module-path,并删除了所有Junit 4兼容性内容,如RunWith()等,并使我的测试纯粹是Junit 5测试。

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

我添加了一个module-info.java(Junit 5不需要一个开放的模块,尽管这些书反过来说明了)

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.

在我模块化测试后,我发现它仍然没有执行ServiceLoader的东西。然后我开始自己寻找故障。

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.

我找到了!可以在基本模块中运行ServiceLoader内容,因为基本模块引用导出的myProvider.jar,后者又访问同一目录中的myProvider-config.properties文件。如果没有此配置文件,myProvider将无法正常工作。

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.

另一方面,有问题的测试模块引用了myProvider的eclipse项目而不是其导出的.jar文件,因此无法找到其配置文件并退出。我将此配置文件从Netbeans移动到Eclipse,只需将其复制到同一目录中即可。因此缺少配置文件是问题所在。

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

更改项目设置我可以毫无失败地运行测试。

I would like to thank all the contributors who responded.

我要感谢所有回复的贡献者。