2

Soo我认为这很简单,但后来:

我有一些小测试。它测试 CDI 依赖注入:

//Imports
@RunWith(Arquillian.class)
public class EditCustomerTest
{
     @Deployment
     public WebArchive createTestArchive()
     {
        return ShrinkWrap
           .create(WebArchive.class, "testcrm.war")
           .addClass(CustomerListProducer.class)
           .addPackage("company.product.controller")
           .addPackage("company.product.model")
           .addPackage("company.product.util")
           .addPackage("company.product.services")
           .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
           .addAsResource("test-ds.xml", "ds.xml")
           .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml");
      }

      @Inject 
      CustomerEditController customerEditController;

      @Inject
      List<Customer> customers;

      @Test
      public void testInjectionResolution(){
           Assert.assertNotNull(customerEditController);
           //do some stuff, where actually nothing happens
      }
 }

CustomerEditController 注入了一个私有的 CustomerListController,而后者本身也注入了一个私有的 CustomerDetailsController。
所有控制器都是 SessionScoped (我知道我不应该,但无论如何它都是一个原型项目,我也无法让事件运行。)
资源是一个自定义类,用于提供 Logger、EntityManager for Persistence 和 FacesContext for错误信息。所有控制器都在“company.product.controller”包中

当我现在将此测试作为标准 JUnit 测试 ( Alt++ , Shift)运行时,我收到错误消息:XT

org.jboss.arquillian.container.spi.client.container.DeploymentException: 无法部署到容器: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"testCDI.war\".WeldService" = > "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"testCDI.war\".WeldService: org.jboss.weld.exceptions.DeploymentException: WELD-001408类型[CustomerDetailsController]的依赖关系不满足限定符 [@Default] 在注入点 [[field] @Inject company.product.controller.CustomerListController.customerDetailsController]"}}

我试图在 addClasses 调用中显式添加所有控制器,但不幸的是结果没有变化。

编辑:

这是一个骨架化的 CustomerListProducer:

@ApplicationScoped
public class CustomerListProducer implements Serializable{

     @Inject
     CustomerService customerServiceBean;

     private static final long serialVersionUID = 1L;
     private List<Customer> customers = new ArrayList<Customer>();
     private Random rnd;
     //some private static final String[] to create DummyData from

     @PostConstruct
     public void init(){
         //check if database is empty, and if then generate DummyData and persist them
     }

     //these call the ServiceBeans implementation to persist the changes
     //the qualifiers are declared in class Events in the package company.product.util
     public void onCustomerAdded(@Observes @Added Customer customer);
     public void onCustomerDeleted(@Observes @Deleted Customer customer);
     public void onCustomerUpdated(@Observes @Updated Customer customer);

     @Named
     @Produces
     public List<Customer> getCustomers();
}

控制器的工作方式几乎相同,注释相同,所以我将在此处发布其中一个:

@Named
@SessionScoped
public class  CustomerDetailsController implements Serializable {
     private static final long serialVersionUID = 1L;

     private Customer customer = new Customer();

     // Inject dependent Controllers. there are no events to pass data between views yet
     @Inject
     ContractEditController contractEditController;
     @Inject
     AddContactPersonController addContactPersonController;

     @Inject
     Resources res;

     @Named
     @Produces
     public Customer getCustomer();

     //CRUD-Events for the Customer that are fired, to persist modifications
}

这里是服务:

@Named
@ApplicationScoped
public interface CustomerService{
    public List<Customer> getAllCustomers();
    public void addCustomer(Customer c);
    public void deleteCustomer(Customer c);
    public void updateCustomer(Customer c);
}

这是相应的实现:

@Stateless
public class CustomerServiceBean implements CustomerService{
    @Inject
    EntityManager entityManager;

    //implementations for the CustomerService Methods, using the Entity Manager
}

编辑 2:
在注释了有问题的注入 CustomerDetailsController 之后(即使我真的需要它),我收到一条新的错误消息:Could not inject CDI Bean
稍微移动 StackTrace 后,我发现不包括持久性,所以我调整了 @Deployment 方法。不幸的是,现在我得到了错误,即我的持久性单元找不到数据源。

我仔细检查了名字,我很肯定它们是一样的。

4

2 回答 2

0

经过数小时的搜索,我发现(如问题中所述) CustomerListProducer 和服务包中使用的休眠数据源配置不正确。

感谢@rubenlop88,我能够以正确的方式添加数据源。这解决了潜在的冲突。我仍然从缺少的服务包中得到很多错误。

为了使创建新测试更容易并集中测试归档创建,我引入了一个新的抽象类,它为所有测试执行此操作:

@RunWith(Arquillian.class)
public abstract class TestBundle{
     @Deployment
     public static WebArchive createTestArchive() {
         return ShrinkWrap
            .create(WebArchive.class, "testArchive.war")
            .addClass(CustomerListProducer.class)
            .addPackage("company.product.controller")
            .addPackage("company.product.model")
            .addPackage("company.product.util")
            .addPackage("company.product.services")
            .addPackage("company.product.test")
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
            .addAsWebInfResource("test-ds.xml", "product-ds.xml")
            .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml");
     }
}

这个类被所有测试扩展,因此包生成所需的任何更改都可以集中进行

于 2013-12-04T09:17:27.487 回答
0

很明显,CustomerDetailsController 上有一个限定符注释 (@Named),它可以防止它被默认注入获取。这就是 Weld 警告的内容。您需要从 bean 中删除限定符或将其添加到您的注入点。

于 2015-11-10T15:53:04.827 回答