Ger*_*cke 5 java unit-testing serviceloader java-9 module-info
我正在使用Java9模块系统(在openjdk11上运行)
我有
此类的单元测试尝试加载两个测试服务实现
测试实现列在 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());
}
}
Run Code Online (Sandbox Code Playgroud)
和一个
src/main/module-info.java
module example {
uses example.Service;
exports example;
}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的单元测试
src/main/example/ServiceTest.java
public ServiceTest {
@Test
void loadServices_should_find_TestServices{
List<Service> services = Service.loadServices();
assertEquals(2, services.size());
}
}
Run Code Online (Sandbox Code Playgroud)
我在测试源中有两个测试服务:
src/main/example/TestService1.java
public TestService1 implements Service {}
Run Code Online (Sandbox Code Playgroud)
src/main/example/TestService2.java
public TestService2 implements Service {}
Run Code Online (Sandbox Code Playgroud)
src/test/resources/META-INF/services/example.Service
example.TestService1
example.TestService2
Run Code Online (Sandbox Code Playgroud)
我使用 maven-surefire-plugin 3.0.0-M3 没有任何特定配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
Run Code Online (Sandbox Code Playgroud)
它正确地修补了示例模块(来自surefireargs文件)
--patch-module example="D:\\git\\example\\target\\test-classes"
Run Code Online (Sandbox Code Playgroud)
当我直接在IntelliJ中执行测试时,测试运行成功,找到了两个服务。但是,当我在maven中构建模块并通过surefire执行测试时,它找不到服务并且测试失败。
我应该如何配置 Surefire 来查找位于测试源中的 TestServices?我无法在模块信息中定义“提供...”声明,因为它们是测试服务。我究竟做错了什么?
我找到了一个解决方法,我并不真正认为这是问题的实际解决方案:在surefire中禁用ModulePath,恢复为ClassPath:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1062 次 |
| 最近记录: |