如何将测试类添加到导入的ear文件并使用arquillian运行服务器端?

时间:2013-02-05 17:16:33

标签: jboss-arquillian

我想使用arquillian创建集成测试。作为部署,我想使用也用于在生产中部署的耳朵。

所以这是我的部署:

@Deployment(testable = true)
public static Archive<?> createDeployment() {
    return ShrinkWrap
            .create(ZipImporter.class, "test.ear")
            .importFrom(new File("simple-webservice-ear-1.0.0-SNAPSHOT.ear"))
            .as(EnterpriseArchive.class);
}

当我运行我的测试类时,我得到一个java.lang.ClassNotFoundException,因为找不到测试类。我知道我可以在部署上设置testable = false但是持久性扩展不起作用:请参阅arquillian persistence extension doesn't work

我该如何解决这个问题? 有没有办法将我的测试类添加到部署中?或者我应该以另一种方式创建我的部署?

2 个答案:

答案 0 :(得分:3)

您可以手动将测试类添加到耳内的战争中,如

WebArchive war = ear.getAsType(WebArchive.class, "/mywarname.war");
war.addClass(MyTestClass.class);

答案 1 :(得分:3)

您可以使用Cheesus提供的方式。当我处理现有的EAR时,我更喜欢将运行测试的WAR与我在特殊JAR中放入的实际测试以及其他测试EJB分开。我的部署如下所示:

@Deployment
public static EnterpriseArchive createDeployment() {

    String path = System.getProperty(EAR_PATH);
    File f = new File(path);
    EnterpriseArchive ear = ShrinkWrap.createFromZipFile(EnterpriseArchive.class, f);

    final JavaArchive foobarEjb = ShrinkWrap.create(JavaArchive.class, "foobarEjb.jar");

    foobarEjb.addClasses(
                    MyTest1.class, 
                    MyTest2.class);
    ear.addAsModule(foobarEjb);


    final WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
            .addAsWebInfResource("WEB-INF/web.xml")
            .addAsWebResource("index.xhtml");

    ear.addAsModule(Testable.archiveToTest(war));

    modifyApplicationXML(ear);
    modifyJBossDeploymentStructure(ear);


    return ear;
}

请注意修改application.xmljboss-deployment-structure.xml的方法。我需要这些来将JAR作为EAR内部的EjbModule初始化。

示例如何更改application.xml

private static void modifyApplicationXML(EnterpriseArchive ear) {
    Node node = ear.get("META-INF/application.xml");

    DescriptorImporter<ApplicationDescriptor> importer =  Descriptors.importAs(ApplicationDescriptor.class, "test");
    ApplicationDescriptor desc = importer.fromStream(node.getAsset().openStream());

    String xml = desc.exportAsString();

    // remove lib definition
    xml = xml.replaceAll("<library-directory>.*<\\/library-directory>", "");

    desc = (ApplicationDescriptor) importer.fromString(xml);
    // append foobar test ejbs
    desc.ejbModule("foobarEjb.jar");
    // append test war
    desc.webModule("test.war", "/test");
    // append lib definition
    desc.libraryDirectory("lib/");

    Asset asset = new StringAsset(desc.exportAsString());

    ear.delete(node.getPath());
    ear.setApplicationXML(asset);

}