问题
I tried to inject my EJB bean in Arquillian integration tests with WebSphere embedded container. When injecting EJB I get the NullPointerException. But container is started correctly, because translogs are occured. I followed the exmaple at the https://github.com/arquillian/arquillian-container-was/blob/master/was-embedded-8/src/test/java/org/jboss/arquillian/container/was/embedded_8/WebSphereEmbeddedIntegrationClientTestCase.java
My test code:
package com.daoleen.websphere.arquillian.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import javax.ejb.EJB;
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.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.daoleen.websphere.arquillian.beans.MyBean;
import com.daoleen.websphere.arquillian.beans.repository.MyBeanRepository;
@RunWith(Arquillian.class)
public class SimpleArquillianTest {
@EJB(lookup="ejblocal:ejb/session/MyBeanRepository")
private MyBeanRepository myBean;
@Deployment
public static JavaArchive createDeployment() {
JavaArchive archive = ShrinkWrap.create(JavaArchive.class, "ArqTest.jar")
.addPackage(SimpleArquillianTest.class.getPackage())
.addPackage(MyBean.class.getPackage())
.addPackage(MyBeanRepository.class.getPackage())
.addAsResource("META-INF/beans.xml")
.addAsResource("logging.properties")
.addAsResource("META-INF/ejb-jar.xml")
.addAsResource("META-INF/ibm-ejb-jar-bnd.xml");
System.out.println("Archive contains: " + archive.toString(true));
return archive;
}
@Test
public void testSuccess() {
assertEquals("success", "success");
}
@Test
public void failTest() {
assertNull(null);
}
@Test
public void testBean() {
assertEquals("hello", myBean.hello());
}
}
MyBean.java
package com.daoleen.websphere.arquillian.beans;
import javax.ejb.Local;
import javax.ejb.Stateless;
import com.daoleen.websphere.arquillian.beans.repository.MyBeanRepository;
@Stateless
@Local(MyBeanRepository.class)
public class MyBean implements MyBeanRepository {
@Override
public String hello() {
System.out.println("Hello bean");
return "hello";
}
}
MyBeanRepository.java
package com.daoleen.websphere.arquillian.beans.repository;
import javax.ejb.Local;
@Local
public interface MyBeanRepository {
public String hello();
}
ibm-ejb-jar-bnd.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_0.xsd"
version="1.0">
<session name="MyBean">
<interface class="com.daoleen.websphere.arquillian.beans.repository.MyBeanRepository" binding-name="ejblocal:ejb/session/MyBeanRepository"/>
</session>
</ejb-jar-bnd>
If I deploy the output _DEFAULT___DEFAULT__ArqTest.jar file into WebSphere App Server, the binding for my bean works correct:
Does anyone know how to inject EJB in the arquillian test? Thanks in advance
UPD: I found when EJB does not implements any interfaces than injection works. But if EJB implements any interface (with or without @Local) injection doesn't works. I can't understand why. Example: The bean written as
@Stateless
public class MyBean {
public String hello() {
System.out.println("Hello bean");
return "hello";
}
}
Works fine, but
@Stateless
public class MyBean implements MyBeanRepository {
@Override
public String hello() {
System.out.println("Hello bean");
return "hello";
}
}
doesn't work
来源:https://stackoverflow.com/questions/30013911/arquillian-was-embedded-8-runs-but-cant-inject-ejb-nullpointerexception