如何使用Junit 5测试服务提供商实施模块? [英] How can I test a service provider implementation module with Junit 5?

查看:174
本文介绍了如何使用Junit 5测试服务提供商实施模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

这是我的示例实现模块,它为MyProvider实现提供了MyProviderImpl

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;
    }

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

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从class-path到module-path,还删除了所有Junit 4兼容性内容,如RunWith()等,并使我的测试纯粹是Junit 5测试。

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.

我是添加了一个module-info.java(Junit 5不需要一个开放的模块,虽然书籍反过来说)

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

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

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内容,因为基本模块引用导出的myProvider.jar,后者又访问同一目录中的myProvider-config.properties文件。如果没有此配置文件,myProvider将无法正常工作。

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.

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

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.

这篇关于如何使用Junit 5测试服务提供商实施模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆