とても簡単だと思いましたが、次のようになります。
小さなテストがあります。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 です (すべきではないことはわかっていますが、どちらにしてもプロトタイプ プロジェクトであり、まだイベントを実行できませんでした。)
Resources は、Logger、Persistence 用の EntityManager、および FacesContext 用のカスタム クラスです。エラーメッセージ。すべてのコントローラはパッケージ「company.product.controller」に含まれています
このテストを標準 JUnit テスト ( Alt
+ Shift
+ X
, T
) として実行すると、次のエラー メッセージが表示されます。
org.jboss.arquillian.container.spi.client.container.DeploymentException: コンテナーにデプロイできませんでした: {"JBAS014671: 失敗したサービス" => {"jboss.deployment.unit.\"testCDI.war\".WeldService" = > "org.jboss.msc.service.StartException サービス 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();
}
コントローラーはすべてほぼ同じように機能し、注釈も同じであるため、ここにそのうちの 1 つを投稿します。
@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 メソッドを調整しました。残念ながら、persistenceunit がデータソースを見つけられないというエラーが表示されます。
名前を再確認しましたが、それらが同じであることは確かです。