unit-testing a ejb3.0 which has another ejb injected

后端 未结 2 533
别跟我提以往
别跟我提以往 2021-01-13 12:01

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

2条回答
  •  醉话见心
    2021-01-13 12:08

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

    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
    
        }
    }
    

    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.

    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.

    The maven dependencies might look like so:

      
        
          org.apache.openejb
          javaee-api
          6.0-3-SNAPSHOT
          provided
        
        
          junit
          junit
          4.8.1
          test
        
        
        
          org.apache.openejb
          openejb-core
          4.0.0-beta-1
          test
        
    
      
    

提交回复
热议问题