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
)运行时,我收到错误消息:X
T
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 方法。不幸的是,现在我得到了错误,即我的持久性单元找不到数据源。
我仔细检查了名字,我很肯定它们是一样的。