测试类路径中的 Gradle Weld SE beans.xml 导致依赖项不满足
语境
运行创建单个 bean 的新实例的单元测试仅在 beans.xml 类路径放置的某些变体中导致不满足的依赖项异常。我已将场景简化为下面的片段。如果您需要更多信息,请告诉我,但这已经足够了。
package foo.bar;
public class MyBean {
//Nothing here.
}
package foo.bar;
import org.junit.jupiter.api.Test;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
class BeanTest {
@Test
void createBean() {
final Weld weld = new Weld();
final WeldContainer container = weld.initialize();
final MyBean bean = containerl.select(MyBean.class).get();
}
}
描述
如果只有一个 beans.xml 文件,则需要将其复制到 src/main 类路径中。如果有两个,它们被复制的顺序很重要。我想知道的是:
- 当只有一个时,为什么 Weld 在 test-classpath 中找不到/搜索 beans.xml?
- 当有 2 个 beans.xml 文件时,为什么 Weld 需要首先将 beans.xml 文件复制到 test-classpath,如果只有一个,它会完全忽略 test-classpath 上的文件?
- 如果 Weld 找到两个 beans.xml 文件(一个在 main 上,一个在 test 上),它将以哪个 beans.xml 文件结束?
当然,我在这里假设这是由于 Weld SE。但是,这可能是由于 gradle 或此设置的其他部分。任何输入表示赞赏。
版本
摇篮:6.5.1
爪哇:11.0.8
窗户:10 家
依赖项
dependencies {
implementation group: 'jakarta.enterprise', name: 'jakarta.enterprise.cdi-api', version: '3.0.0'
testImplementation group: 'org.jboss.weld.se', name: 'weld-se-core', version: '4.0.0.Final'
}
复制到 Gradle 类路径
从 src/main/resources
作品classes {
doLast {
copy {
from('src/main/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/main/"
}
}
}
classes {
doLast {
copy {
from('src/main/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/test/"
into "$buildDir/classes/java/main/"
}
}
}
不工作
classes {
doLast {
copy {
from('src/main/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/test/"
}
}
}
classes {
doLast {
copy {
from('src/main/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/main/"
into "$buildDir/classes/java/test/"
}
}
}
从 src/test/resources
作品classes {
doLast {
copy {
from('src/test/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/main/"
}
}
}
classes {
doLast {
copy {
from('src/test/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/test/"
into "$buildDir/classes/java/main/"
}
}
}
不工作
classes {
doLast {
copy {
from('src/test/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/test/"
}
}
}
classes {
doLast {
copy {
from('src/test/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/main/"
into "$buildDir/classes/java/test/"
}
}
}
从 src/main/resources 和 src/test/resources
作品classes {
doLast {
copy {
from('src/test/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/test/"
from('src/main/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/main/"
}
}
}
不工作
classes {
doLast {
copy {
from('src/main/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/main/"
from('src/test/resources') { include 'META-INF/beans.xml' }
into "$buildDir/classes/java/test/"
}
}
}