Wildfly 8 및 NetBeans를 사용한 테스트
문제
NetBeans와 Maven에서 Wildfly 테스트를하는 데 문제가 있습니다.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입니다. 이 텍스트는 수많은 기사, 이 하나의 그러나 여기에 제안 된 솔루션은 더 나은 배포에 대한 테스트를 위해 테스트를 위해 동일한 Wildfly 설치를 사용하고 있습니다. < / P>
다음 힌트를주의 깊게 따라갈 필요가 있습니다. 그런 다음 작동합니다.
단계 제로 - 준비 : 테스트를 위해 사용할 수있는 것을 준비합시다.
일반 Maven Javaee Project, I.e :
demoproj
demoproj-ear
demoproj-ejb
demoproj-war
.
NetBeans를 사용하고 있지만 다른 IDE에서도 다음 단계는 매우 다르게되어서는 안됩니다. 테스트 할 무언가를 갖기 위해서는 Entity Bean, Persistence Unit 및 Session Bean과 같은 일부 물건을 만들어 봅시다. NetBeans, 데이터베이스를 사용하는 엔티티 및 엔티티에서 세션 빈을 사용하여 데모 지속성 단위를 만들었습니다.
단계 : 우리는 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의 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 단계 : 거의 더 중요하지만, 하나의 중요한 것은 everoproj-ejb에서 src/test/resources
폴더를 생성하고 persistence.xml
에서 src/main/resources/META-INF
를 복사하고 test-persistence.xml
에서 이름을 바꿉니다.
데이터베이스가 실행 중인지, 유효한 드라이버가 설치되었는지 확인하고, 올바른 데이터 소스가 사용됩니다. 내 경우 대부분의 문제로 인해이 종류의 문제로 인해 발생했습니다.
이제! 이제는 demoproj
에서 깨끗하고 빌드해야하며 모든 것이 괜찮을 경우 익숙해지는 정상적인 NetBeans 테스트 기능을 사용할 수 있습니다. 즐기십시오!
update 원격 서버 또는 동일한 시스템에서 원격 서버 또는 실행 인스턴스를 사용하는 것을 원한다면 특정 성능 이득으로 인해 권장하는 것이 무엇인지, 다음 부분을 변경해야합니다. 코드 :
마스터 프로젝트의 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에서 동일합니다.