在WebLogic 10.3.5上使用Arquillian注入@EJB不起作用

时间:2012-06-25 13:43:49

标签: ejb-3.0 weblogic-10.x jboss-arquillian

我正在使用Arquillian在WebLogic 10.3.5上运行基本单元测试。不幸的是,@ EJB注入对我不起作用,并在调用EJB时​​得到NullPointerException。

我发现的示例不需要添加ejb-jar.xml,但我添加了一个。无论如何,都会抛出NullPointerException。

有什么想法吗?

单元测试:

import static org.testng.Assert.assertEquals;
import java.io.File;
import javax.ejb.EJB;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.testng.annotations.Test;
import eu.ema.eudract.results.DummyService;
import eu.ema.eudract.results.DummyServiceBean;

public class ResultStatefulServiceBeanIntegrationTest extends Arquillian {

    @Deployment
    public static JavaArchive createDeployment() {
        JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClasses(DummyService.class, DummyServiceBean.class)
                .addAsManifestResource("META-INF/ejb-jar.xml", "ejb-jar.xml");

        System.out.println(archive.toString(true));
        archive.as(ZipExporter.class).exportTo(new File("C:\\Users\\fragkakm\\Desktop\\test.jar"), true);
        return archive;
    }

    @EJB
    private DummyService dummyService;

    @Test
    public void loadResultTest() {
        assertEquals(dummyService.doNothing(), "Did nothing!");
    }
}

输出:

test.jar:
/META-INF/
/META-INF/ejb-jar.xml
/eu/
/eu/ema/
/eu/ema/eudract/
/eu/ema/eudract/results/
/eu/ema/eudract/results/DummyServiceBean.class
/eu/ema/eudract/results/DummyService.class
25 Ιουν 2012 4:31:35 μμ org.jboss.arquillian.container.wls.WebLogicDeployerClient deploy
INFO: Starting weblogic.Deployer to deploy the test artifact.
25 Ιουν 2012 4:31:42 μμ org.jboss.arquillian.container.wls.WebLogicDeployerClient forkWebLogicDeployer
INFO: weblogic.Deployer appears to have terminated successfully.
25 Ιουν 2012 4:31:45 μμ org.jboss.arquillian.container.wls.WebLogicDeployerClient undeploy
INFO: Starting weblogic.Deployer to undeploy the test artifact.
25 Ιουν 2012 4:31:51 μμ org.jboss.arquillian.container.wls.WebLogicDeployerClient forkWebLogicDeployer
INFO: weblogic.Deployer appears to have terminated successfully.
FAILED: loadResultTest
java.lang.NullPointerException
    at eu.ema.eudract.results.result.ResultStatefulServiceBeanIntegrationTest.loadResultTest(ResultStatefulServiceBeanIntegrationTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:182)
    at org.jboss.arquillian.testng.Arquillian$2.invoke(Arquillian.java:167)
    at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
    at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
    at org.jboss.arquillian.container.test.impl.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
    at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:90)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
    at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:111)
    at org.jboss.arquillian.testng.Arquillian.run(Arquillian.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:194)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:695)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
    at org.testng.TestNG.run(TestNG.java:1030)
    at org.jboss.arquillian.testng.container.TestNGTestRunner.execute(TestNGTestRunner.java:53)
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:160)
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:126)
    at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:90)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:183)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3717)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)

我的EJB接口:

package eu.ema.eudract.results;

import javax.ejb.Remote;

@Remote
public interface DummyService {

    String doNothing();
}

EJB实现:

package eu.ema.eudract.results;

import javax.ejb.Stateless;

import eu.ema.eudract.results.DummyService;

@Stateless
public class DummyServiceBean implements DummyService {

    public String doNothing() {
        return "Did nothing!";
    }

}

arquillian.xml

<?xml version="1.0"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://jboss.org/schema/arquillian"
    xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <container qualifier="arquillian-wls-remote-10.3" default="true">
        <configuration>
            <property name="adminUrl">t3://localhost:7001</property>
            <property name="adminUserName">weblogic</property>
            <property name="adminPassword">aaaaaaaa</property>
            <property name="wlsHome">C:\dbin\wls1035_dev\wlserver\</property>
            <property name="target">AdminServer</property>
        </configuration>
    </container>

</arquillian>

我添加的依赖项:

<dependency>
    <groupId>org.jboss.arquillian</groupId>
    <artifactId>arquillian-bom</artifactId>
    <version>1.0.1.Final</version>
    <scope>import</scope>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>org.jboss.arquillian.container</groupId>
    <artifactId>arquillian-wls-remote-10.3</artifactId>
    <scope>test</scope>
    <version>1.0.0.Alpha2</version>
</dependency>

1 个答案:

答案 0 :(得分:2)

WLS 10.3.5是符合JEE5标准的容器,因此您不能将EJB驻留在Web模块中(自JEE6起支持,即WLS 12c)。

为了允许在WLS 10.3.x中将EJB注入到测试类中,您需要在ShrinkWrap ejb-local-ref的web.xml中提供关联的WebArchive条目。如果您准备了EAR存档,那么您需要将这些条目添加到WebArchive中,这些条目将使用Arquillian Servlet Testrunner进行丰富。

Arquillian GitHub存储库in the WebLogicInjectionTestCase类中提供了一个示例。关联的web.xml文件为in-container-web-eartest.xml

请注意,当您尝试注入本地EJB时,肯定需要ejb-local-ref条目,因为默认情况下WLS不会在JNDI树中绑定它们。您可以跳过远程EJB的这个先决条件。