单元测试一个ejb注入的ejb3.0 [英] unit-testing a ejb3.0 which has another ejb injected

查看:109
本文介绍了单元测试一个ejb注入的ejb3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何单元测试ProcessorBean?
由于我只是不测试ProcessorBean而不是Dao,所以我需要存根或模拟Dao,但是我不知道如何用Junit来做到这一点。

How can I unit test the ProcessorBean? Since I only wan't to test the ProcessorBean and not the Dao, I need to stub or mock the Dao, but I have no idea how I could do that with Junit.

我正在使用Junit4和Ejb3.0

I'm using Junit4 and Ejb3.0

@Stateless
public class ProcessorBean {

    @EJB
    private Dao dao;

    public void process() {
        //logic to be tested
    }
}


推荐答案

OpenEJB中有一些支持可能会与嘲笑结合使用。

There's some support in OpenEJB you might find useful in combination with mocking.

作为EJB 3.0 Embedded EJBContainer API的替代方法,您可以直接在代码中构建应用程序。

As an alternative to the EJB 3.0 Embedded EJBContainer API, you can simply build your app up in code.

import junit.framework.TestCase;
import org.apache.openejb.jee.EjbJar;
import org.apache.openejb.jee.StatelessBean;
import org.apache.openejb.junit.ApplicationComposer;
import org.apache.openejb.junit.Module;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.ejb.EJB;

@RunWith(ApplicationComposer.class)
public class ProcessorBeanTest extends TestCase {

    @EJB
    private ProcessorBean processorBean;

    @Module
    public EjbJar beans() {
        EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new StatelessBean(ProcessorBean.class));
        ejbJar.addEnterpriseBean(new StatelessBean(MockDao.class));
        return ejbJar;
    }

    @Test
    public void test() throws Exception {

        // use your processorBean

    }
}

这里我们看到由$ code ApplicationComposer运行的测试用例。它是一个JUnit测试运行器的简单包装器,可以查找可用于定义应用程序的 @Module 方法。

Here we see a testcase run by the ApplicationComposer. It is a simple wrapper for a JUnit test runner that looks for @Module methods which can be used to define your app.

实际上OpenEJB已经进行了多年的内部测试,并且在最近几个版本中决定开放(从3.1.3开始)。它超越了强大而且非常快的速度,因为它会削减类路径扫描和部分更重的部署。

This is actually how OpenEJB has done all its internal testing for years and something we decided to open up in the last few releases (since 3.1.3). It's beyond powerful and extremely fast as it cuts out classpath scanning and some of the heavier parts of deployment.

maven依赖关系可能如下所示:

The maven dependencies might look like so:

  <dependencies>
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0-3-SNAPSHOT</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>
    <!--
    The <scope>test</scope> guarantees that none of your runtime
    code is dependent on any OpenEJB classes.
    -->
    <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>openejb-core</artifactId>
      <version>4.0.0-beta-1</version>
      <scope>test</scope>
    </dependency>

  </dependencies>

这篇关于单元测试一个ejb注入的ejb3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆