el *_*00b 5 maven-3 maven reflections
简单的流程要求: 在包中找到的所有扩展名List.classjava.util。
这是我正在使用的来源:
Reflections reflections = new Reflections("java.util");
Set<Class<?>> subtypes = reflections.getSubTypesOf(List.class);
Run Code Online (Sandbox Code Playgroud)
很简单,对吧?
它既可以在IntelliJ IDEA中也可以在Eclipse中使用,但是当我通过Maven运行测试时不起作用。我尝试使用所org.reflections.util.ConfigurationBuilder提供的方法添加内容,以添加URL和过滤包名称,但没有运气。
有什么建议么?
我浏览了这篇文章,但无法正常工作:“ 仅当由Maven执行时,使用Reflections谷歌库的单元测试才会失败 ”
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ohno</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
Sample.java
package com.example.uhoh;
import org.reflections.Reflections;
import java.util.Set;
public class Sample {
@SuppressWarnings ("unchecked")
public static Set<Class<?>> lookup(Class<?> type) {
Reflections reflections = new Reflections("java.util");
return (Set<Class<?>>) reflections.getSubTypesOf(type);
}
}
Run Code Online (Sandbox Code Playgroud)
SampleTest.java
package com.example.uhoh;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
import java.util.Set;
@Test
public class SampleTest {
public void testSample() {
Set<Class<?>> result = Sample.lookup(List.class);
Assert.assertNotNull(result, "NULL returned.");
Assert.assertFalse(result.isEmpty(), "Unable to find any extensions of java.util.List");
}
}
Run Code Online (Sandbox Code Playgroud)
Maven输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.example.uhoh.SampleTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@4e515669
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.261 sec <<< FAILURE!
(com.example.uhoh.SampleTest) Time elapsed: 0.071 sec <<< FAILURE!
java.lang.AssertionError: Unable to find any extensions of java.util.List expected [false] but found [true]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertFalse(Assert.java:63)
at com.example.uhoh.SampleTest.testSample(SampleTest.java:15)
Results :
Failed tests: (com.example.uhoh.SampleTest): Unable to find any extensions of java.util.List expected [false] but found [true]
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
Run Code Online (Sandbox Code Playgroud)
代码 public void testSample(){Class [] dataTypes = {ArrayList.class,LinkedList.class,List.class,AbstractList.class,Collection.class,Map.class,Properties.class};
Arrays.stream(dataTypes).parallel().forEach(next -> {
Set<Class<?>> result = Sample.lookup(next);
Assert.assertNotNull(result, "NULL returned.");
Assert.assertFalse(result.isEmpty(), "Unable to find any extensions of java.util.List");
});
}
Run Code Online (Sandbox Code Playgroud)
Maven输出
java.lang.IllegalStateException: java.lang.IllegalStateException: zip file closed
我刚刚在 Reflections 库(版本 0.9.11)中遇到了同样的问题,只有在从 Maven 构建执行单元测试时。对我的 Surefire 插件的简单 POM 文件更改为我解决了这个问题。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
该<useSystemClassLoader>参数默认为“真”的配置。强制它为 'false' 似乎解决了我的单元测试中的类加载器问题。
要解决您的问题,请使用 ReflectionsConfigurationBuilder手动设置包含该类的 jar 的 URL。这适用于 IntelliJ,并且当使用命令行 Maven 调用 Surefire 插件时,mvn test.
示例.java
public static Set<Class<?>> lookup(Class<?> type) {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
// For List.class, this will be a path to rt.jar.
configurationBuilder.addUrls(ClasspathHelper.forClass(type));
Reflections reflections = new Reflections(configurationBuilder);
return (Set<Class<?>>) reflections.getSubTypesOf(type);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
873 次 |
| 最近记录: |