Java9:ServiceLoader不';t从测试源(模块)加载测试实现
我正在使用Java9模块系统(在openjdk11上运行) 我有Java9:ServiceLoader不';t从测试源(模块)加载测试实现,java,unit-testing,java-9,serviceloader,module-info,Java,Unit Testing,Java 9,Serviceloader,Module Info,我正在使用Java9模块系统(在openjdk11上运行) 我有 从Java8迁移而来,代码包含使用ServiceLoader机制的类 此类的单元测试,尝试加载两个测试服务实现 测试实现列在META-INF/services文件中 src/main/example/Service.java public interface Service { public static List<Service> loadServices(){ return StreamSuppor
- 从Java8迁移而来,代码包含使用ServiceLoader机制的类李>
- 此类的单元测试,尝试加载两个测试服务实现
- 测试实现列在META-INF/services文件中
src/main/example/Service.java
public interface Service {
public static List<Service> loadServices(){
return StreamSupport.stream(ServiceLoader.load(Service.class).spliterator(),false)
.collect(Collectors.toList());
}
}
module example {
uses example.Service;
exports example;
}
public ServiceTest {
@Test
void loadServices_should_find_TestServices{
List<Service> services = Service.loadServices();
assertEquals(2, services.size());
}
}
public TestService1 implements Service {}
public TestService2 implements Service {}
我有一个这样的单元测试
src/main/example/ServiceTest.java
public interface Service {
public static List<Service> loadServices(){
return StreamSupport.stream(ServiceLoader.load(Service.class).spliterator(),false)
.collect(Collectors.toList());
}
}
module example {
uses example.Service;
exports example;
}
public ServiceTest {
@Test
void loadServices_should_find_TestServices{
List<Service> services = Service.loadServices();
assertEquals(2, services.size());
}
}
public TestService1 implements Service {}
public TestService2 implements Service {}
src/main/example/TestService2.java
public interface Service {
public static List<Service> loadServices(){
return StreamSupport.stream(ServiceLoader.load(Service.class).spliterator(),false)
.collect(Collectors.toList());
}
}
module example {
uses example.Service;
exports example;
}
public ServiceTest {
@Test
void loadServices_should_find_TestServices{
List<Service> services = Service.loadServices();
assertEquals(2, services.size());
}
}
public TestService1 implements Service {}
public TestService2 implements Service {}
src/test/resources/META-INF/services/example.Service
example.TestService1
example.TestService2
我使用的是maven surefire插件3.0.0-M3,没有任何特定配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
当我直接在IntelliJ中执行测试时,测试成功运行,找到了两个服务。
但是,当我在maven中构建模块并由surefire执行测试时,它没有找到服务,测试失败
我应该如何配置surefire以查找位于测试源中的TestServices?我无法在模块信息中定义“提供…”声明,因为它们是测试服务。我做错了什么我发现了一个解决办法,我没有真正考虑这个问题的实际解决方案: 在surefire中禁用ModulePath,恢复到类路径:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
org.apache.maven.plugins
maven surefire插件
假的
要测试TestService1和TestService2,需要使用相应的提供的
子句将它们放入自己的测试模块中。JDK没有一个--add提供了选项来扩展模块提供的服务集。有这样一个选项是有缺陷的,因为服务绑定是解决方案的一部分,并且在处理用于扩展模块图的命令行选项之前很久就完成了。顺便看看ServiceLoader::stream方法,这会将代码片段减少为:ServiceLoader.load(service.class).stream().collect(Collectors.toList());代码>