使用arquillian测试远程客户端jndi查找

问题描述 投票:0回答:2

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

我目前正在将EJB 2.x的EJB应用程序迁移到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
2个回答
0
投票

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

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

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


0
投票

我遇到了同样的问题,所以在一个变通方法中我只是将要测试的方法添加到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;
© www.soinside.com 2019 - 2024. All rights reserved.