質問

私はNetBeansとMavenの下でワイルドテストをする問題があります。私はIDEからアプリケーションを起動することができます、私はNetBeansデバッガとそれをデバッグすることさえできますが、単体テストを開始するとすぐに、次のエラーが発生します。

No EJBContainer provider available: no provider names had been found.
javax.ejb.EJBException
    at javax.ejb.embeddable.EJBContainer.reportError(EJBContainer.java:216)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:146)
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:102)
    at cz.interactsys.ejb.StatefulSessionBeanTest.setUp(StatefulSessionBeanTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
.

コードは次のような標準です。

    int numberA = 1;
    int numberB = 2;
    EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
    MyBean instance = (MyBean) container.getContext().lookup("java:global/classes/MyBean");
    int expResult = 3;
    int result = instance.plusOne(1);
    assertEquals(expResult, result);
    container.close();
.

事前にありがとうございました。

役に立ちましたか?

解決

問題は、WildflyにEJBContainerがまったくないということです。私は、ejbContainerがプラットフォーム仕様に従ってあるべきであることを考えると、これがどのように可能であるかわからない。それにもかかわらず、私たちは違うものを使う必要があり、それはarquillianです。このテキストは他の数多くの記事に触発されました、maily しかし、ここで示唆されたソリューションは、私にとって良い展開のためのテストのための同じワイルドなインストールを使用しています。< / P>

以下のヒントに注意深く追跡する必要があります。それからそれはうまくいくでしょう。

ステップゼロ - 準備:テストに使用できるものを準備しましょう。

通常のMaven Javaeeプロジェクト、すなわち。:

demoproj
demoproj-ear
demoproj-ejb
demoproj-war
.

NetBeansを使用していますが、他のIDESでも次の手順は非常に異ならないでください。テストするものを持つために、エンティティBean、永続化単位、およびセッションBeanのようなものを作成しましょう。私は、NetBeans、データベースを使用しているエンティティ、およびエンティティからのセッションBeanを使用してデモ永続性ユニットを作成しました。

ステップ1: pom.xml od demoproj(すなわちマスタープロジェクト)に次のコードを追加します:

<properties>
    <junit-version>4.11</junit-version>
    <arquillian-version>1.1.4.Final</arquillian-version>
    <arquillian-wildfly-version>8.1.0.Final</arquillian-wildfly-version>
    <arquillian-transaction-version>1.0.1.Final</arquillian-transaction-version>  
    <javaee-server-home>/Java/wildfly-8.1.0.Final</javaee-server-home>
</properties>        
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee-api-version}</version>
        </dependency>
        <!-- -JUNIT-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit-version}</version>
        </dependency>
        <!-- rquillian itself-->
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>${arquillian-version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <!-- this is in an extention to arquillian it is optional-->
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-transaction-bom</artifactId>
            <version>${arquillian-transaction-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- this is container adapter for wildfly-->
        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-arquillian-container-embedded</artifactId>
            <version>${arquillian-wildfly-version}</version>
        </dependency>
        <!-- this is the wildfly emb.container - BUT eventually it is not a fully blown emb.container-->
        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-embedded</artifactId>
            <version>${arquillian-wildfly-version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <pluginManagement>
        <plugins>
            <!-- compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compiler-plugin-version}</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- maven ear plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>${ear-plugin-version}</version>
                <configuration>
                    <generateModuleId>true</generateModuleId>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- ejb plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>${ejb-plugin-version}</version>
                <configuration>
                    <ejbVersion>${ejb-spec-version}</ejbVersion>
                </configuration>
            </plugin>
            <!-- war plugin -skinny wars mode! -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>${war-plugin-version}</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>    
.

ステップ2: demoproj-ejbのpom.xmlの依存関係に次のように追加します。

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.junit</groupId>
        <artifactId>arquillian-junit-container</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-arquillian-container-embedded</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-embedded</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.arquillian.extension</groupId>
        <artifactId>arquillian-transaction-jta</artifactId>
        <scope>test</scope>
    </dependency>        
.

demoproj-ejbのプラグインセクションに追加します。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-Dfile.encoding=UTF-8</argLine>
                <forkCount>1C</forkCount>
                <systemPropertyVariables>
                    <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                    <jboss.home>${javaee-server-home}</jboss.home>
                </systemPropertyVariables>
                <redirectTestOutputToFile>false</redirectTestOutputToFile>
            </configuration>
        </plugin> 
.

ステップ3:テスト単位を作成しましょう。通常のIDE機能を使用すると、次のようなものがあります。

package com.demopack.demoproj;

import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class DemoBeanLocalTest {

    public DemoBeanLocalTest() {
    }

    @Before
    public void setUp() {
    }

    @Test
    public void testBusinessMethod() {
    }

}
.

現在クラスのヘッダーの直前に@RunWith(Arquillian.class)を追加する必要があります。デフォルトのコンストラクタを削除して、テストクラス内に次のコードを追加します。

@EJB
DemoBeanLocal demoBean;

@Deployment
public static JavaArchive createDeployment() {

    JavaArchive jar = ShrinkWrap.create(JavaArchive.class);
    jar.addAsResource("test-persistence.xml", "META-INF/persistence.xml");
    jar.addPackage("com.demopack.demoproj");
    jar.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

    return jar;
}
.

それからあなたはいつものようにいくつかのテスト方法を追加することができました。テストクラス全体が次のようになります。

package com.demopack.demoproj;

import javax.ejb.EJB;
import junit.framework.Assert;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class DemoBeanLocalTest {

    @EJB
    DemoBeanLocal demoBean;

    @Deployment
    public static JavaArchive createDeployment() {

        JavaArchive jar = ShrinkWrap.create(JavaArchive.class);
        jar.addAsResource("test-persistence.xml", "META-INF/persistence.xml");
        jar.addPackage("com.demopack.demoproj");
        jar.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

        return jar;
    }

    @Before
    public void setUp() {
    }

    @Test
    public void testBusinessMethod() {

        Assert.assertEquals("test test", demoBean.businessMethod("test"));
    }

}
.

ステップ4:ほとんど完了しましたが、もう1つの重要なことが残っています.demoproj-ejbの下でsrc/test/resourcesフォルダを作成し、persistence.xmlからsrc/main/resources/META-INFをコピーし、それをtest-persistence.xmlに名前を変更します。

データベースが実行されていることを確認し、有効なドライバがインストールされ、正しいデータソースが使用されています。私の場合、ほとんどのトラブルはこの種の問題によって引き起こされました。

それはそれです!これでdemoprojに清潔で構築する必要があり、すべて問題ない場合は、使用するときに通常のNetBeansテスト機能を使用できます。楽しい!

更新リモートインスタンス(リモートサーバまたは同じマシン上の実行中のインスタンスのどちらか)を使用している場合は、ある程度のパフォーマンスの向上により推奨するものが、以下の部分を変更する必要があります。コード:

マスタープロジェクトのpom.xmlの場合:

交換:

`<artifactId>wildfly-arquillian-container-embedded</artifactId>` for
`<artifactId>wildfly-arquillian-container-remote</artifactId>`.
.

削除:

<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-embedded</artifactId>
    <version>${arquillian-wildfly-version}</version>
</dependency>
.

demoproj-ejbのpom.xmlで同じことをします。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top