在Seam项目中运行测试,该测试不是由seam-gen生成的

时间:2011-01-31 16:29:46

标签: java seam integration-testing testng

我首先使用EJB 2编写了一个项目,然后迁移到Spring,然后迁移到JBoss Seam 2.2.0(这是我正在处理的情况)。它将运行在Tomcat上,但现在它是在JBoss 4.2上执行的(尽管我相信这个事实是无关紧要的)。我的任务是运行上次迁移之前编写的所有测试。

经过大量的谷歌搜索,我写了这样的话:

public class CustomUserDAOTest extends SeamTest {
    @Test
    public void f() throws Exception {
        new ComponentTest() {
            @Override
            protected void testComponents() throws Exception {
                CustomUserDAO customUserDAO = (CustomUserDAO) Component.getInstance(CustomUserDAOBean.class);
                List<CustomUser> users = customUserDAO.getAll();
                assertNotNull(users);
            }
        }.run();
    }
}

这很好,因为我设法获得了CustomUserDAOBean及其依赖项的实例,但是当我运行测试时,我遇到了另一个问题:

java.lang.RuntimeException: exception invoking: getTransaction
    at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:154)
    at org.jboss.seam.Component.callComponentMethod(Component.java:2249)
    at org.jboss.seam.Component.unwrap(Component.java:2275)
    at org.jboss.seam.Component.getInstance(Component.java:2041)
    [OMITTED]
    at org.jboss.seam.intercept.JavaBeanInterceptor.invoke(JavaBeanInterceptor.java:103)
    at br.com.visent.sicorp.server.dao.impl.CustomUserDAOBean_$$_javassist_seam_1.listAll(CustomUserDAOBean_$$_javassist_seam_1.java)
    at br.com.visent.sicorp.server.dao.test.CustomUserDAOTest$1.testComponents(CustomUserDAOTest.java:24)
    at org.jboss.seam.mock.AbstractSeamTest$ComponentTest.run(AbstractSeamTest.java:162)
    [OMITTED]
    at org.testng.TestNG.run(TestNG.java:856)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
    [OMITTED]
    at org.jboss.seam.util.Reflections.invokeAndWrap(Reflections.java:144)
    ... 45 more

我在网上发现了一些关于它的评论但没有解决方案。我该怎么办?有人有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我已经按照上面的Shervin的建议并得到了解决方案。实际上,当我们使用seam-gen创建项目时,src/test目录将包含一个readme.txt,其中包含以下内容:

If you want to run tests using the Eclipse TestNG plugin, you'll need to add
these jars to the top of your TestNG classpath. Using the Run Dialog, select the
XML suite to run, and select these entries from the project tree:

/lib/test/jboss-embedded-all.jar
/lib/test/hibernate-all.jar
/lib/test/thirdparty-all.jar
/lib/jboss-embedded-api.jar
/lib/jboss-deployers-client-spi.jar
/lib/jboss-deployers-core-spi.jar

You also need to add the Embedded JBoss bootstrap folder, which you can do by
clicking on the Advanced... button.

/bootstrap

Seam uses JBoss Embedded in its unit and integration testing. This has an
additional requirement when using JDK 6. Add the following VM argument to the VM
args tab in the TestNG launch configuration for your suite.

-Dsun.lang.ClassLoader.allowArraySyntax=true 

Please be sure to use JDK 6 Update 4 or better (>= 1.6.0_04) if you are using
JDK 6. The Update 4 release upgraded to JAXB 2.1 which removes a problem with
earlier versions of Sun's JDK 6 which required overriding the JAXB libraries
using the endorsed directory. 

To add tests to your project create a TestNG xml descriptor called *Test.xml
(e.g. FooTest.xml) next to your test classes and run ant test.

我打开了测试的“运行配置”(可以通过工具栏上的“运行”按钮,单击小黑色向下箭头并选择“运行配置”),添加上面列出的罐子。 “Classpath”选项卡(jboss-deployers-client-spi.jar除外,这是有问题的)并将-Dsun.lang.ClassLoader.allowArraySyntax=true添加到“Arguments”选项卡中的“VM Arguments”,因为我使用的是Java 6.

我仍有一些问题,但这个特定问题已经解决了。