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



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

这就是我的部署:

@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持久性扩展dos';不起作用。

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

您可以使用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);
}

您可以像一样手动将测试类添加到ear内部的war中

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

最新更新