简体   繁体   English

如何测试使用企业应用程序的EJB模块

[英]How can I test EJB module that use an Enterprise Application

I have this scenario: 我有这种情况:

  1. Enterprise Application. 企业应用。 Runs ok. 运行正常。 --> contains MyFacade - >包含MyFacade
  2. Client application. 客户申请。 Runs ok. 运行正常。
  3. Ejb module. Ejb模块。 Runs ok, but can't test --> contains MyClass.java 运行正常,但无法测试 - >包含MyClass.java
  4. DomainLibrary. DomainLibrary。 --> contains entities and remote interfaces. - >包含实体和远程接口。

I want to run test file of point 3. (ejb module). 我想运行第3点的测试文件。(ejb模块)。 Ok, first of all I put the Enterprise Application running. 好的,首先我将企业应用程序运行起来。 On second place I run the test file of the point 3. The problem is that the remote interface ejb contained on Enterprise Application can't be found. 在第二位,我运行第3点的测试文件。问题是无法找到企业应用程序中包含的远程接口ejb。

Error 1: It end up with an endless loop of the following output 错误1:最终得到以下输出的无限循环

WARNING: AS-CDI-005 Okt 22, 2013 4:49:23 PM org.glassfish.weld.BeanDeploymentArchiveImpl handleEntry

Error 1: Solved Running JUnit Tests on embedded glassfish 4 causing WARNING: AS-CDI-005 错误1:解决了 在嵌入式glassfish 4上运行JUnit测试导致警告:AS-CDI-005

Error 2!!!: 错误2 !!!:

javax.naming.NamingException: Lookup failed for [...] (MyFacade)

Ejb module: MyClass.java Ejb模块:MyClass.java

@Singleton
public class MyClass implements MyClassLocal {

    @EJB(lookup = "java:global/EnterpriseApplication-ejb/MyFacade!com.mydomain.repository.MyFacadeRemote")
    private MyFacadeRemote myFacade;

    public MyClass() {
    }

    public void bussinesMethod(){
        System.out.println("Hello stackOverWorld! ");
    myFacade.findAll();
    }
}

Test method: 测试方法:

@Test
public void testBusinessMethod() throws Exception {
    System.out.println("businessMethod");

    Map<Object, Object> properties = new HashMap<Object, Object>(); 
    properties.put(EJBContainer.APP_NAME, "MyEjbModule");
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    EJBContainer container = EJBContainer.createEJBContainer(properties);
    MyClassLocal instance = (MyClassLocal)container.getContext().lookup("java:global/MyEjbModule/classes/MyClass!com.mydomain.MyClassLocal");
    //EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
    //MyClassLocal instance = (MyClassLocal)container.getContext().lookup("java:global/classes/MyClass");

    instance.businessMethod();
    container.close();
}

You may use the same approach we did for our EJB application. 您可以使用我们为EJB应用程序执行的相同方法。 You may call some of the tests 'unit tests' but they are really integration tests. 你可以调用一些测试'单元测试',但它们实际上是集成测试。 Running these tests includes starting the application server first, then running the JUnit test cases and finally stopping the server. 运行这些测试包括首先启动应用程序服务器,然后运行JUnit测试用例,最后停止服务器。 These test cases act as the client, loading the ejb remote interface, make the appropriate calls, validate the return values, etc just like your client would. 这些测试用例充当客户端,加载ejb远程接口,进行适当的调用,验证返回值等,就像客户端一样。

One of the problems with this is that these tests can take a while to run, depending on how many you have. 其中一个问题是这些测试可能需要一段时间才能运行,具体取决于您拥有的测试数量。 It also requires the server be up and running. 它还要求服务器启动并运行。

I know there must be better ways, but this has worked for our project for about 10 years. 我知道必须有更好的方法,但这已经为我们的项目工作了大约10年。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM