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

Jboss arquillian 如何将测试类添加到导入的ear文件并使用arquillian运行服务器端?,jboss-arquillian,Jboss Arquillian,我想使用arquillian创建集成测试。作为部署,我希望使用ear,它也用于在生产中部署 这就是我的部署: @Deployment(testable = true) public static Archive<?> createDeployment() { return ShrinkWrap .create(ZipImporter.class, "test.ear") .importFrom(new File("simple-

我想使用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);
}
部署(可测试=真) 公共静态存档createDeployment(){ 回缩包装 .create(ZipImporter.class,“test.ear”) .importFrom(新文件(“simple-webservice-ear-1.0.0-SNAPSHOT.ear”)) .as(企业级); } 当我运行测试类时,我得到一个java.lang.ClassNotFoundException,因为找不到测试类。我知道我可以在部署中设置testable=false,但是持久性扩展不起作用:请参阅

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

您可以手动将测试类添加到ear中的war中

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

您可以使用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.xml
jboss部署结构.xml
的方法。我需要这些属性来将JAR作为EAR中的EJB模块进行初始化

示例如何更改
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);

}
private static void modifyApplicationXML(EnterpriseArchive ear){
Node=ear.get(“META-INF/application.xml”);
DescriptorImporter=描述符.importAs(ApplicationDescriptor.class,“测试”);
ApplicationDescriptor desc=importer.fromStream(node.getAsset().openStream());
字符串xml=desc.exportAsString();
//删除库定义
xml=xml.replaceAll(“”,“”);
desc=(ApplicationDescriptor)importer.fromString(xml);
//附加foobar测试EJB
描述ejb模块(“foobarEjb.jar”);
//附加测试战
desc.webModule(“test.war”和“/test”);
//附加库定义
desc.libraryDirectory(“lib/”);
资产资产=新的StringAsset(desc.exportAsString());
delete(node.getPath());
setApplicationXML(资产);
}