【问题标题】:WELD SE with JUnit 5 require enabling bean discovery programmatically带有 JUnit 5 的 WELD SE 需要以编程方式启用 bean 发现
【发布时间】:2019-07-24 21:16:38
【问题描述】:

我正在设置一个示例项目用于测试目的,它使用 Weld SE 和 JUnit5,出于某种原因,在我的测试类中,在初始化焊接后,我观察到,由于某种原因,它的 bean 发现被禁用,在最后,它导致我出现这个错误:

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Person with qualifiers @Default;

这是我的测试课:

@EnableWeld
public class SimpleTestA {

@Inject
Person p;

@Test
public void testThatItWorks() {
    System.out.println("Hey");
 }
}

位于:

projectName\core\model\src\test\java\com\aCompany\projectName\core\model\testmodel\SimpleTestA.java

这是我的 beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
   instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1" bean-discovery-mode="all" />

位于:

projectName\core\model\src\main\resources\META-INF\beans.xml

项目结构相当简单,我只有一个名为“projectName”的主模块,它是名为“core”的子模块的父模块,其中包含我之前粘贴的所有内容。我的依赖列表是这样的:

 <dependencies>

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>${junit-jupiter.aggregator.version}</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.jboss.weld.se</groupId>
  <artifactId>weld-se-core</artifactId>
  <version>${weld.se.core.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.jboss.weld/weld-junit5 -->
<dependency>
  <groupId>org.jboss.weld</groupId>
  <artifactId>weld-junit5</artifactId>
  <version>${weld.junit5.version}</version>
  <scope>test</scope>
</dependency>

这些是我的属性:

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<weld.se.core.version>3.0.1.Final</weld.se.core.version>
<weld.junit5.version>1.3.1.Final</weld.junit5.version>
<junit-jupiter.aggregator.version>5.4.0</junit-jupiter.aggregator.version>

如果我修改测试添加带有显式 bean 发现激活的焊接初始化属性,一切都会很好:

    @WeldSetup
WeldInitiator weldInitiator =     WeldInitiator.of(WeldInitiator.createWeld().enableDiscovery());

我错过了什么?如果 beans.xml 存在,bean 发现不应该自动激活吗?提前谢谢你。

【问题讨论】:

    标签: java junit5 weld-se


    【解决方案1】:

    所以这里的问题是您使用的是 Weld SE(嗯,weld-junit 是),而不是您可能从 WildFly 等服务器知道的 Weld EE。

    在 SE 中,默认情况下发现是关闭的,因此使用所谓的合成存档。 合成存档仅包含您自己提供的任何内容 - 作为 bean 的类、要扫描的包等。它不会扫描整个类路径。

    因此,要使您的示例正常工作,您可以开启发现功能,也可以通过Weld.addPackages()Weld.addClasses() 等添加所需的类和包。 在 Weld-junit 的上下文中,这转换为 WeldInitiator.createWeld().addPackages()

    Weld SE(和weld-junit)不执行整个发现的原因是因为您将有效地扫描整个类路径,包括JDK 包和所有。这需要时间,除此之外,您还会发现大量不需要的豆子。或者你可以选择你不想要的拦截器/替代品。最后但同样重要的是,这些都是单元测试,因此测试您的 bean 的部署最少。

    【讨论】:

    • 非常感谢您给我如此明确的答复。
    猜你喜欢
    • 2018-05-04
    • 2016-04-22
    • 2015-07-27
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多