在 Arquillian 测试中注入 class 始终为 null

Injecting in Arquillian test class always null

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

Arquillian 对我的项目的依赖是通过项目模板创建的,我没有修改任何东西。然后我创建了我的第一个测试 class 作为

@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.doWork(ConcurrentValidator.java:68)
    at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:66)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:60)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory.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 的实例。最可能的原因是缺少一个或多个依赖项。

也许您需要添加:

archive.addClass(CustomerDaoImpl.class);