简体   繁体   English

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

[英]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.这是我的基本模块,它需要 myspi 包中定义的接口的实现。 Various providers can offer MyProvider implementations.各种提供者可以提供 MyProvider 实现。 Base module uses them via myspi.MyProvider interface implementation.基本模块通过 myspi.MyProvider 接口实现使用它们。

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

This is my sample implementation module which provides the MyProvider implementation with MyProviderImpl这是我的示例实现模块,它通过 MyProviderImpl 提供 MyProvider 实现

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).但是相同的代码在 Junit 5 测试代码中返回空列表(ServiceLoader 返回 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 测试服务提供者模块。或者是否有任何替代 Junit 允许我们创建测试模块(模块化测试 API),在模块信息中声明“使用 myspi.MyProvider”并与 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. 您需要说服Java模块系统,当您将模块解析为测试运行时时,测试模块才是最重要的。

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中维护专用的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. 并且希望大多数IDE也不支持您。 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. 我已经将Jun​​it从类路径删除到模块路径,还删除了所有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. 在对测试进行模块化之后,我发现它仍然没有执行ServiceLoader的东西。 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. 可以在基本模块中运行ServiceLoader东西,因为基本模块引用了导出的myProvider.jar,而后者又访问了同一目录中的myProvider-config.properties文件。 Without this config file myProvider cannot work properly. 没有此配置文件,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. 另一方面,有问题的测试模块引用了myProvider的eclipse项目,而不是其导出的.jar文件,因此找不到其配置文件并退出。 I'd moved this config file from Netbeans to Eclipse simply copying it into the same directory. 我已经将该配置文件从Netbeans移至Eclipse,只需将其复制到同一目录中即可。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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