ejb 2.0 3.0_Java EE 6测试第I部分– EJB 3.1可嵌入API

ejb 2.0 3.0

我们从Enterprise JavaBeans开发人员那里听到的最常见的请求之一就是需要改进的单元/集成测试支持。
EJB 3.1规范引入了EJB 3.1可嵌入API,用于在Java SE环境中执行EJB组件。

与传统的基于Java EE服务器的执行不同,可嵌入式用法允许客户端代码及其对应的企业bean在同一JVM和类加载器中运行。 这为测试,脱机处理(例如批处理)以及在桌面应用程序中使用EJB编程模型提供了更好的支持。
[…]可嵌入的EJB容器为托管环境提供了对Java EE运行时中存在的相同基本服务的支持:注入,对组件环境的访问,容器管理的事务等。通常,企业bean组件不了解他们在其中运行的一种托管环境。 这使得企业组件在各种测试和部署方案中都具有最大的可重用性,而无需进行大量的返工。

让我们来看一个例子。

首先创建一个Maven项目,然后添加可嵌入的GlassFish依赖项。
我选择使用TestNG测试框架,但JUnit应该也能正常工作。

<dependencies>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.4</version>
        <scope>test</scope>
    </dependency>
    <!--
        The javaee-api is stripped of any code and is just used to
        compile your application. The scope provided in Maven means
        that it is used for compiling, but is also available when
        testing. For this reason, the javaee-api needs to be below
        the embedded Glassfish dependency. The javaee-api can actually
        be omitted when the embedded Glassfish dependency is included,
        but to keep your project Java-EE 6 rather than GlassFish,
        specification is important.
    -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

这是一个简单的Stateless会话Bean:

@Stateless
public class HelloWorld {
    public String hello(String message) {
        return "Hello " + message;
    }
}

它通过无接口视图公开业务方法。
它没有可用于嵌入执行的特殊API。

这是一些在可嵌入容器中执行Bean的测试代码:

public class HelloWorldTest {
    private static EJBContainer ejbContainer;

    private static Context ctx;

    @BeforeClass
    public static void setUpClass() throws Exception {
        // Instantiate an embeddable EJB container and search the
        // JVM class path for eligible EJB modules or directories
        ejbContainer = EJBContainer.createEJBContainer();

        // Get a naming context for session bean lookups
        ctx = ejbContainer.getContext();
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
        // Shutdown the embeddable container
        ejbContainer.close();
    }

    @Test
    public void hello() throws NamingException {
        // Retrieve a reference to the session bean using a portable
        // global JNDI name
        HelloWorld helloWorld = (HelloWorld)
                ctx.lookup("java:global/classes/HelloWorld");

        // Do your tests
        assertNotNull(helloWorld);
        String expected = "World";
        String hello = helloWorld.hello(expected);
        assertNotNull(hello);
        assertTrue(hello.endsWith(expected));
    }
}

源代码在GitHub上ejb31-embeddable文件夹下可用。

有关JPA示例的分步教程,请阅读使用嵌入式EJB容器从NetBeans文档测试企业应用程序

尽管此新API向前迈了一大步,但我仍然对这种方法有疑问:您正在将容器进行测试。 这需要一个与您的生产环境不同的专用容器。

Java EE 6测试第二部分中 ,我将介绍ArquillianShrinkWrap
Arquillian是一个强大的面向容器的测试框架,位于TestNG和JUnit之上,使您能够在您选择的容器上创建生产环境,并仅在该环境中执行测试(使用数据源,JMS目标以及许多其他工具)。您希望在生产环境中看到的其他配置)。 Arquillian不会将测试引入运行时,而是将您的测试引入了运行时。

相关文章

参考: Java EE 6测试第I部分–来自我们JCG合作伙伴 Samuel Santos的EJB 3.1可嵌入API ,位于Samaxes博客上。


翻译自: https://www.javacodegeeks.com/2012/06/java-ee-6-testing-part-i-ejb-31.html

ejb 2.0 3.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值