我想有一个JUnit(或其他)系统,可以在模拟环境中测试企业bean,以便在本地注入所有资源/ bean等.企业bean不应该完成这个.应支持各种注射.
我想有一个maven插件,以便测试可以从maven构建运行.
在单元测试期间不需要事务,这将需要一个完整的容器.
你知道这样的maven插件或测试框架吗?有经验吗?
不一定是最简单的方法,但我设法使用JBoss的可嵌入EJB3.0容器,在Java Persistence with Hibernate的主要帮助下
首先,我不知道maven插件/依赖项,所以我必须在本地安装自己的:
(1)jboss-EJB-3.0_Embeddable_ALPHA_9.zip
从下载中下载
(2)此zipfile包含两个重要的JAR:
jboss-ejb3-all.jar
thirdparty-all.jar
(3)将这些JAR安装到本地存储库中(使用您自己的首字母而不是nkl
):
mvn install:install-file \ -Dfile=jboss-ejb3-all.jar \ -DgroupId=nkl.jboss.embedded \ -DartifactId=jboss-ejb3-all \ -Dversion=3.0-alpha-9 mvn install:install-file \ -Dfile=thirdparty-all.jar \ -DgroupId=nkl.jboss.embedded \ -DartifactId=thirdparty-all \ -Dversion=3.0-alpha-9
(4)如果一切顺利,您现在应该在本地存储库中有两个目录.
(5)现在将以下依赖项添加到您的 pom.xml
nkl.jboss.embedded jboss-ejb3-all 3.0-alpha-9 nkl.jboss.embedded thirdparty-all 3.0-alpha-9
(6)我还需要为HSQLDB和SLF4J添加依赖项
(7)您需要conf
将zipfile目录中的一些文件复制到src/main/resources
项目目录中:
default.persistence.properties
ejb3-interceptors-aop.xml
embedded-jboss-beans.xml
jndi.properties
(8)我创建了自己beans.xml
和persistence.xml
文件src/main/resources/META-INF
:
java:/messageDS org.hsqldb.jdbcDriver jdbc:hsqldb:test 0 10 1000 30000 org.hibernate.ejb.HibernatePersistence java:/messageDS
(9)我使用此处的附加信息创建了如下的单元测试.
package org.nkl; import java.io.File; import javax.naming.InitialContext; import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; import org.jboss.ejb3.embedded.EJB3StandaloneDeployer; import org.junit.Test; import org.nkl.model.MessageHandler; public class BasicTest { @Test public void testEjb() throws Exception { EJB3StandaloneBootstrap.boot(null); EJB3StandaloneBootstrap.deployXmlResource("META-INF/beans.xml"); EJB3StandaloneDeployer deployer = EJB3StandaloneBootstrap .createDeployer(); deployer.getArchives().add(new File("target/classes").toURI().toURL()); deployer.create(); deployer.start(); InitialContext context = new InitialContext(); MessageHandler messageHandler = (MessageHandler) context .lookup("MessageHandlerBean/local"); messageHandler.saveMessages(); messageHandler.showMessages(); EJB3StandaloneBootstrap.shutdown(); } }
(10)这使用简单的SLSB,如:
package org.nkl.ejb; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.nkl.model.Message; import org.nkl.model.MessageHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Stateless public class MessageHandlerBean implements MessageHandler { @PersistenceContext private EntityManager em; Logger logger = LoggerFactory.getLogger(MessageHandlerBean.class); public void saveMessages() { logger.info("In saveMessages"); em.persist(new Message("Hello World")); } @SuppressWarnings("unchecked") public void showMessages() { logger.info("In showMessages"); List messages = em.createQuery( "select m from Message m order by m.text asc").getResultList(); for (Object o : messages) { logger.info(((Message) o).getText()); } } }
(11)和一个Message实体类如:
package org.nkl.model; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name = "MESSAGES") public class Message { @Id @GeneratedValue @Column(name = "MESSAGE_ID") private Long id; @Column(name = "MESSAGE_TEXT") private String text; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "NEXT_MESSAGE") private Message nextMessage; public Message(String text) { this.text = text; } Message() {} // setters/getters not shown. }
让我们说这个方法沿途有许多绊脚石.
值得注意的是依赖顺序.
祝好运!!