How to unit test EJB3 without a container

Unit test EJB3 without a container has become much easier since the EJB 3.1 spec with the introduction of the embeddable EJBContainer concept. An embeddable EJBContainer is a container for enterprise beans that does not require a Java EE server to run.

Interesting usage scenarios are:

  • EJB unit testing: you don’t need to install a JavaEE server for EJB development, unit testing and deployment to the container
  • Lightweight: the embeddable container has a much smaller footprint
  • Fast: starts faster than the full server, because it only initializes EJB-related components

Sample code

Below you can find a simple setup method for a JUnit test that configures the Embeddable EJBContainer.

@Before
public void setup() {
	Properties properties = new Properties();
	properties.setProperty(EJBContainer.MODULES, "myModule");
	properties.put(EJBContainer.PROVIDER, "tomee-embedded");
	Context context = EJBContainer.createEJBContainer().getContext();
}

@After
public void tearDown() throws NamingException {
	ejbContainer.close();
}

Here myModule is the module-name defined in ejb-jar.xml file

So how does the EJBContainer start an embedded EJB container? You’ll need to provide an embedded EJB container on the classpath. Normally all Application servers supporting JavaEE6 have to provide such an embedded EJB container.

Apache TomEE container This example uses Apache TomEE as it provides an easy way to specify the correct dependencies via Maven.

<dependency>
	<groupId>org.apache.openejb</groupId>
	<artifactId>tomee-embedded</artifactId>
	<version>1.5.2</version>
</dependency>            
<dependency>
	<groupId>javax</groupId>
	<artifactId>javaee-api</artifactId>
	<version>6.0</version>
	<scope>provided</scope>
</dependency>

The properties.put(EJBContainer.PROVIDER, “tomee-embedded”) makes sure that we will use Apache TomEE when running the test. (even if there is another provider on the classpath)

0 thoughts on “How to unit test EJB3 without a container”

  1. 台湾的虚拟号码:通信技术的新时代

    近年来,技术进步正在迅速改变人们的沟通方式。 其中一项创新是虚拟号码的出现,为用户提供了新的沟通机会。 台湾作为亚洲领先的技术中心之一,正积极迎接这一趋势。

    什么是虚拟号码?

    虚拟号码是未链接到特定物理设备或位置的电话号码。 它可以在互联网上工作,可以在各种设备上使用,如智能手机,计算机甚至特殊应用程序。 这为用户开辟了全新的视野。

    虚拟房间在台湾的优势

    1. 灵活性:用户可以根据需要轻松更改他们的号码,这对企业特别有用。

    2. 节省:虚拟号码通常比传统号码便宜,特别是对于国际电话。 对于在国际市场运营的公司尤其如此。

    3. 安全性和匿名性:虚拟号码有助于保持隐私。 用户可以隐藏他们的真实号码,这对自由职业者和企业家尤其重要。

    4. 访问各种服务:大多数虚拟号码提供商提供额外的功能,如语音信箱,应答机和记录对话的能力。 http://www.168x.com.cn/20240129/42156688.html

    对业务和通信的影响

    虚拟数字正成为台湾企业的重要工具。 它们使您能够有效地管理客户电话,确保高质量的服务。 此外,公司可以通过使用虚拟号码进入新市场来轻松扩大客户群。

    结论

    虚拟数字正在成为台湾和世界各地现代通信的一个组成部分. 通信技术的新时代为个人和企业开辟了新的机会和改变了沟通方式。 台湾凭借其高水平的技术和创新,处于这一转型的最前沿。

  2. 台湾的虚拟号码:通信技术的新时代

    近年来,技术进步正在迅速改变人们的沟通方式。 其中一项创新是虚拟号码的出现,为用户提供了新的沟通机会。 台湾作为亚洲领先的技术中心之一,正积极迎接这一趋势。

    什么是虚拟号码?

    虚拟号码是未链接到特定物理设备或位置的电话号码。 它可以在互联网上工作,可以在各种设备上使用,如智能手机,计算机甚至特殊应用程序。 这为用户开辟了全新的视野。

    虚拟房间在台湾的优势

    1. 灵活性:用户可以根据需要轻松更改他们的号码,这对企业特别有用。

    2. 节省:虚拟号码通常比传统号码便宜,特别是对于国际电话。 对于在国际市场运营的公司尤其如此。

    3. 安全性和匿名性:虚拟号码有助于保持隐私。 用户可以隐藏他们的真实号码,这对自由职业者和企业家尤其重要。

    4. 访问各种服务:大多数虚拟号码提供商提供额外的功能,如语音信箱,应答机和记录对话的能力。 http://www.eeju.com.cn/20240129/77711342.html

    对业务和通信的影响

    虚拟数字正成为台湾企业的重要工具。 它们使您能够有效地管理客户电话,确保高质量的服务。 此外,公司可以通过使用虚拟号码进入新市场来轻松扩大客户群。

    结论

    虚拟数字正在成为台湾和世界各地现代通信的一个组成部分. 通信技术的新时代为个人和企业开辟了新的机会和改变了沟通方式。 台湾凭借其高水平的技术和创新,处于这一转型的最前沿。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.