4

ユニットテスト用のMavenプロジェクトがあり、CDIを使用したいと思います。私は次のようにpom.xmlにweld-se依存関係を置きました:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se</artifactId>
    <version>1.1.8.Final</version>
</dependency>
<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.0-SP3</version>
</dependency>

JUnitテストランナーで溶接をブートストラップしています:

public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
   private final Class klass;
   private final Weld weld;
   private final WeldContainer container;

   public WeldJUnit4Runner(final Class klass) throws InitializationError {
       super(klass);
       this.klass = klass;
       this.weld = new Weld();
       this.container = weld.initialize();
   }

   @Override
   protected Object createTest() throws Exception {
       final Object test = container.instance().select(klass).get();

       return test;
   }
}

そして、このランナーを使用した単体テスト。このテストでは、アプリケーションスコープのBeanを注入しています。問題は、アプリケーションスコープのBeanが溶接するのがまったく不明であるかのように、唯一の注入ポイントへの「不満足な依存関係」のために溶接を初期化できないことです。しかし、そのBeanは私のテストではsrc / test / java / ...にあります(ただし、別のJavaパッケージにあります)。

src / test/resourcesに空のbeans.xmlがあります。

開始時に溶接が警告を出すことに気づきましたが、これらが私の問題の原因ではないと思います:

604 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
605 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled

誰かがそれを手伝ってくれる?

4

3 に答える 3

6

CDI-Unitをご覧ください。これはRunner、JUnit テスト クラスの を提供します。

@RunWith(CdiRunner.class) // Runs the test with CDI-Unit
class MyTest {
    @Inject
    Something something; // This will be injected before the tests are run!

    ...
}

出典: CDI-Unit ユーザーガイド.

CDI-Unit は以下の警告もログに記録しますが、それでも問題なく動作します。

WARN (InterceptionTypeRegistry.java) - WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
WARN (InterceptionTypeRegistry.java) - WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
于 2014-03-18T08:56:12.907 に答える
4

Couple of things to look at: Weld SE container for Arquillian or the DeltaSpike CdiCtrl module

于 2012-10-26T04:48:02.013 に答える
1

ディレクトリに次beans.xmlを追加します。src/test/resources/META-INF

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    version="1.1" bean-discovery-mode="all">
</beans>

警告の理由: クラスjavax.ejb.PostActivateおよびjavax.ejb.PrePassivateが見つかりませんでした。依存関係がありません。

この依存関係を pom.xml に追加します。

<dependency>
    <groupId>javax.ejb</groupId>
    <artifactId>javax.ejb-api</artifactId>
    <version>3.2</version>
</dependency>

よろしく。

于 2015-08-30T17:13:11.923 に答える