【问题标题】:Injecting in Arquillian test class always null在 Arquillian 测试类中注入始终为空
【发布时间】:2015-08-14 04:30:58
【问题描述】:

我在我的 Eclipse IDE 中安装了 JBoss Tools 插件,并将项目创建为 New > JBoss Central > Java EE EAR Project。我使用了 Wildfly 8.2.0.Final 服务器。我的项目模板的 EJB 模块如下

我的项目的 Arquillian 依赖项是由项目模板创建的,我没有修改任何东西。然后我创建了我的第一个测试类

@RunWith(Arquillian.class)
public class CustomerServiceTest {
    @Inject
    private CustomerDao customerDao;

    @Deployment
    public static Archive<?> createDeployment() {

    JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar")
        .addAsResource("META-INF/test-persistence.xml","META-INF/persistence.xml")
        .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    archive.addClass(CustomerDto.class);
    archive.addClass(Customer.class);
    archive.addClass(CustomerDao.class);
    archive.addClass(Dao.class);

    System.out.println(archive.toString(true));
    return archive;
    }

    @Test
    public void getCustomers() {
    // customerDao Always Null
    System.out.println("############# Testing Success ##################"+ customerDao);
    }
}

这是控制台日志

10:39:19,578 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-7) MSC000001: Failed to start service jboss.deployment.unit."test.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."test.war".WeldStartService: Failed to start service
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_40]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_40]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_40]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDao with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private com.solt.hms.customer.CustomerServiceTest.customerDao
  at com.solt.hms.customer.CustomerServiceTest.customerDao(CustomerServiceTest.java:0)

    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:372)
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:293)
    at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:167)
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:531)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_40]
    ... 3 more

10:39:19,578 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 1) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "test.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"test.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"test.war\".WeldStartService: Failed to start service
    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDao with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private com.solt.hms.customer.CustomerServiceTest.customerDao
  at com.solt.hms.customer.CustomerServiceTest.customerDao(CustomerServiceTest.java:0)
"}}
10:39:19,578 ERROR [org.jboss.as.server] (management-handler-thread - 1) JBAS015870: Deploy of deployment "test.war" was rolled back with the following failure message: 
{"JBAS014671: Failed services" => {"jboss.deployment.unit.\"test.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"test.war\".WeldStartService: Failed to start service
    Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type CustomerDao with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private com.mycom.project.customer.CustomerServiceTest.customerDao
  at com.mycom.project.customer.CustomerServiceTest.customerDao(CustomerServiceTest.java:0)
"}}

我完全是 Arquillian 测试的新手。我发现并研究了许多 Arquillian 测试示例。这些看起来不错,但我的有什么问题?感谢您阅读我的问题。乐于帮助新手。

【问题讨论】:

  • 错误信息表明 JBoss 无法创建 CustomerDao 的实例。最可能的原因是缺少一个或多个依赖项。这个类做任何日志记录吗?如果是这样,那么您需要添加您使用的日志库(除非它基于 java.util.logging)。
  • @SteveC 感谢您的评论先生。我的课程CustomerDao 只是简单的接口,它的实现CustomerDaoImpl 进行DAO 管理活动。先生,这两个班级都没有任何日志记录。
  • 你的archive.addClass(CustomerDaoImpl.class);在哪里?
  • Arrrr....谢谢先生!我忘记了。插入后,我的程序运行良好,先生。感谢您的宝贵时间。

标签: java jakarta-ee jboss jboss-arquillian


【解决方案1】:

错误消息表明 JBoss 无法创建 CustomerDao 的实例。最可能的原因是缺少一个或多个依赖项。

也许你需要补充:

archive.addClass(CustomerDaoImpl.class);

【讨论】:

  • 一个更简单的方法是使用addClasses,它接受一个数组或使用addPackageaddPackages
猜你喜欢
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 1970-01-01
相关资源
最近更新 更多