我如何使用 Junit 测试 return 一个 MongoClient 的 Singleton EJB?
How i can use Junit to test a Singleton EJB that return a MongoClient?
我必须使用 JUnit 测试一个简单的 Singleton EJB,return 一个 MongoClient。
我想我可以试试 Arquillian。
使用此代码,我获得了一个 Java.lang.AssertionError,因为容器未注入 EJB。我哪里错了?
EJBclass:
@Stateless
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class MongoClientProviderBean {
private Logger logger = Logger.getLogger(MongoClientProviderBean.class);
private MongoClient mongoClient = null;
@Lock(LockType.READ)
public MongoClient getMongoClient() {
return mongoClient;
}
@PostConstruct
public void init() {
try {
String address = "mongo-host";
int port = 27017;
//TODO: inject del indirizzo e porta
mongoClient = new MongoClient(address, port);
logger.info("Inizializzato mongoclient ("+address+", "+port+")");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
@PreDestroy
public void tearDown() {
mongoClient.close();
logger.info("Mongoclient chiuso");
}
}
测试Class:
@RunWith(Arquillian.class)
public class MongoClientProviderBeanTest {
private static final Logger logger = Logger.getLogger(MongoClientProviderBean.class);
@EJB
MongoClientProviderBean mongoClientProviderBean;
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "test.jar")
.addClasses(MongoClientProviderBean.class, MongoClient.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
public void getMongoClient() throws Exception {
assertNotNull(mongoClientProviderBean);
/*
dummy test
*/
mongoClientProviderBean.getMongoClient();
}
}
部分 POM:
[...]
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.13.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>2.3.5.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
[...]
</dependencies>
[...]
我也尝试过使用 Wildfly 管理的容器,但我得到了这个异常:
org.jboss.arquillian.container.spi.client.container.LifecycleException: Could not start container
at org.jboss.as.arquillian.container.managed.ManagedDeployableContainer.startInternal(ManagedDeployableContainer.java:168)
at org.jboss.as.arquillian.container.CommonDeployableContainer.start(CommonDeployableContainer.java:123)
at org.jboss.arquillian.container.impl.ContainerImpl.start(ContainerImpl.java:199)
[...]Caused by: java.lang.IllegalArgumentException: WFLYLNCHR0001: The path 'null' does not exist
at org.wildfly.core.launcher.Environment.validateWildFlyDir(Environment.java:313)
at org.wildfly.core.launcher.AbstractCommandBuilder.validateWildFlyDir(AbstractCommandBuilder.java:652)
并且 JUnit 无法启动容器。
单独焊接不会注入 EJB。也许您可以尝试使用扩展焊接的 ejb-cdi-unit,以便像普通 bean 一样注入 ejbs。
我必须使用 JUnit 测试一个简单的 Singleton EJB,return 一个 MongoClient。 我想我可以试试 Arquillian。
使用此代码,我获得了一个 Java.lang.AssertionError,因为容器未注入 EJB。我哪里错了?
EJBclass:
@Stateless @Singleton @ConcurrencyManagement(ConcurrencyManagementType.CONTAINER) public class MongoClientProviderBean { private Logger logger = Logger.getLogger(MongoClientProviderBean.class); private MongoClient mongoClient = null; @Lock(LockType.READ) public MongoClient getMongoClient() { return mongoClient; } @PostConstruct public void init() { try { String address = "mongo-host"; int port = 27017; //TODO: inject del indirizzo e porta mongoClient = new MongoClient(address, port); logger.info("Inizializzato mongoclient ("+address+", "+port+")"); } catch (UnknownHostException e) { e.printStackTrace(); } } @PreDestroy public void tearDown() { mongoClient.close(); logger.info("Mongoclient chiuso"); } }
测试Class:
@RunWith(Arquillian.class) public class MongoClientProviderBeanTest { private static final Logger logger = Logger.getLogger(MongoClientProviderBean.class); @EJB MongoClientProviderBean mongoClientProviderBean; @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class, "test.jar") .addClasses(MongoClientProviderBean.class, MongoClient.class) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); } @Test public void getMongoClient() throws Exception { assertNotNull(mongoClientProviderBean); /* dummy test */ mongoClientProviderBean.getMongoClient(); } }
部分 POM:
[...] <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian</groupId> <artifactId>arquillian-bom</artifactId> <version>1.1.13.Final</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.arquillian.container</groupId> <artifactId>arquillian-weld-ee-embedded-1.1</artifactId> <version>1.0.0.CR9</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.weld</groupId> <artifactId>weld-core</artifactId> <version>2.3.5.Final</version> <scope>test</scope> </dependency> <dependency> <groupId>org.jboss.arquillian.junit</groupId> <artifactId>arquillian-junit-container</artifactId> <scope>test</scope> </dependency> [...] </dependencies> [...]
我也尝试过使用 Wildfly 管理的容器,但我得到了这个异常:
org.jboss.arquillian.container.spi.client.container.LifecycleException: Could not start container
at org.jboss.as.arquillian.container.managed.ManagedDeployableContainer.startInternal(ManagedDeployableContainer.java:168)
at org.jboss.as.arquillian.container.CommonDeployableContainer.start(CommonDeployableContainer.java:123)
at org.jboss.arquillian.container.impl.ContainerImpl.start(ContainerImpl.java:199)
[...]Caused by: java.lang.IllegalArgumentException: WFLYLNCHR0001: The path 'null' does not exist
at org.wildfly.core.launcher.Environment.validateWildFlyDir(Environment.java:313)
at org.wildfly.core.launcher.AbstractCommandBuilder.validateWildFlyDir(AbstractCommandBuilder.java:652)
并且 JUnit 无法启动容器。
单独焊接不会注入 EJB。也许您可以尝试使用扩展焊接的 ejb-cdi-unit,以便像普通 bean 一样注入 ejbs。