【问题标题】:Test the remote client jndi lookup using arquillian使用 arquillian 测试远程客户端 jndi 查找
【发布时间】:2015-05-11 15:11:46
【问题描述】:

设置:arquillian,jboss as 7.1.1.final 作为托管容器

我目前正在将 EJB 应用程序从 EJB 2.x 迁移到 3.x,并将 JBoss 3.x 迁移到 JBoss AS 7.1。 在这个过程中,我想让大多数课程都接受测试,并偶然发现了 arquillian。 虽然 arquillian 似乎在跨 bean 功能上提供了一些不错的功能,但我无法弄清楚使用 jndi 查找测试远程客户端功能是否有效。

我在我的 bean 上使用了 Arquillian 入门指南,但由于这些使用 @Inject 并且在我的应用程序中,我(至少认为我)需要从该路径转向的任何地方都使用 jndi 查找。

这是我基于Arquillian Getting Started 创建的TestCase。我在所有尝试中都明确放弃了使用我认为可能会有所帮助的 jndi 属性。

测试

should_create_greeting()

如果 Greeter bean 使用单独的 Producer 则可以工作。

@RunWith(Arquillian.class)
public class GreeterTest {
    public static final String ARCHIVE_NAME = "test";

    Logger logger = Logger.getLogger(GreeterTest.class.getName());

    @Deployment
    public static Archive<?> createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class, ARCHIVE_NAME + ".jar").addPackage(Greeter.class.getPackage())
            .addAsManifestResource("test-persistence.xml", "persistence.xml").addAsManifestResource("OracleGUIDS-ds.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    return jar;
    }

    /**
     * @Inject works using a producer with {@code @Produces}
     */
     // @Inject
     // Greeter greeter;
     @ArquillianResource
     Context context;

     GreeterRemote greeter;

     @Before
     public void before() throws Exception {
         Map<String, String> env = new HashMap<>();
         env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.as.naming.InitialContextFactory");
         env.put("jboss.naming.client.ejb.context", "true");
         // env.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT",
         // "false");
         // env.put("jboss.naming.client.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS",
         // "false");
         // env.put("jboss.naming.client.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
         // "false");
         for (Map.Entry<String, String> entry : env.entrySet()) {
             context.addToEnvironment(entry.getKey(), entry.getValue());
         }
         greeter = (GreeterRemote) context.lookup(ARCHIVE_NAME + "/" + Greeter.class.getSimpleName() + "!"
            + GreeterRemote.class.getName());
     }

     @Test
     public void should_create_greeting() {
         Assert.assertEquals("Hello, Earthling!", greeter.createGreeting("Earthling"));
         greeter.greet(System.out, "Earthling");
     }

}

是否可以使用 jndi 查找来运行此测试?我错过了什么吗?

【问题讨论】:

    标签: jboss jboss-arquillian


    【解决方案1】:

    如果您想测试 EJB 的远程功能,您可能希望在客户端而不是在容器中运行。

    您可以使用 @Deployment(testable=false) 将部署配置为仅客户端。然后@Test 方法将像您是远程客户端一样运行。

    除此之外,如果需要,您可以通过注入的 Context 查找 bean。

    【讨论】:

    • 感谢您的回复。这在使用我为特定测试部署的托管服务器时是否有效,或者我是否需要运行部署的服务器?我真的很想在测试期间将 bean 部署到托管服务器,然后针对这些服务器运行一些测试。但是,如果我尝试这样做,例如使用两个部署,我似乎无法远程访问 bean。
    【解决方案2】:

    我遇到了同样的问题,所以在解决方法中,我只是将要测试 remoteejb 的方法添加为参数。 在我的 ejb 上:

    public List localBean.obtain(RemoteEJB remoteEjb){
     return remoteEjb.obtain();
    }
    

    然后是 arquillian 测试:

    @Inject
    private LocalBean localBean;
    
    @Inject
    private RemoteEJB remoteEjb;
    
    
      @Test
        public void test(){
           List<Vo>voList =  localBean.obtain(remoteEjb);
        }
    

    最好的部分是它注入的远程ejb和调用者方法的原始

    @EJB(lookup="java:global/ear/ejb/RemoteEjb")
    private RemoteEJB remoteEjb;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多