gpt4 book ai didi

maven - Google Reflections & Maven - 在 Maven 中找不到数据,但在 IDE 中工作

转载 作者:行者123 更新时间:2023-12-04 18:32:29 25 4
gpt4 key购买 nike

问题

简单流程请求:查找 List.class 的所有扩展名在 java.util .

这是我正在使用的来源:

Reflections reflections = new Reflections("java.util");
Set<Class<?>> subtypes = reflections.getSubTypesOf(List.class);

很简单,对吧?

它适用于 IntelliJ IDEA 和 Eclipse,但当我通过 Maven 运行测试时它不起作用。我尝试使用 org.reflections.util.ConfigurationBuilder 添加内容提供了添加 URL 和过滤包名称的方法,但没有运气。

有什么建议么?

我浏览了这篇文章,但无法正常工作:“ Unit test using the Reflections google library fails only when executed by Maven

代码

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>

示例.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);
}
}

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

输出

在 IDE 中工作
Sample Working in IDE

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

并行错误

代码
公共(public)无效 testSample() {
类 [] 数据类型 = {
ArrayList.class、LinkedList.class、
List.class,AbstractList.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");
});
}

Maven 输出
java.lang.IllegalStateException: java.lang.IllegalStateException: zip file closed

最佳答案

我刚刚在使用反射库(版本 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>
<useSystemClassLoader>配置参数默认为“真”。强制它为“假”似乎可以解决我的单元测试中的类加载器问题。

关于maven - Google Reflections & Maven - 在 Maven 中找不到数据,但在 IDE 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38270824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com