如何将测试类添加到导入的Ear文件中并使用Arquillian运行服务器端? [英] How to add test classes to an imported ear file and run server side with arquillian?

查看:55
本文介绍了如何将测试类添加到导入的Ear文件中并使用Arquillian运行服务器端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create integration tests using arquillian. As deployment i want to use the ear that is also used to deploy in production.

这是我的部署:

@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持久性扩展不起作用.

When i run my test class I get a java.lang.ClassNotFoundException because the test class is not found. I know I can set testable=false on the deployment but then the persistence extension doesn't work: see arquillian persistence extension doesn't work.

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

How can i solve this problem? Is there a way to add my test class to the deployment? Or should i create my deployment in another way?

推荐答案

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

You can use the way provided by Cheesus. When I am dealing with existing EAR, I prefer to separate the WAR that runs the tests, from the actual tests that I put in special JAR along with other testing EJBs. My deployment looks like this:

@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.

Notice the methods to modify the application.xml and jboss-deployment-structure.xml. I need these to propertly initialize the JAR as an EjbModule inside the EAR.

示例如何更改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文件中并使用Arquillian运行服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆