How to add test classes to an imported ear file and run server side with arquillian?

女生的网名这么多〃 提交于 2019-11-29 22:38:42

问题


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

So this is my deployment:

@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);
}

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?


回答1:


You can manually add the test class to the war inside the ear like

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



回答2:


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;
}

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.

Example how do I change 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);

}


来源:https://stackoverflow.com/questions/14713129/how-to-add-test-classes-to-an-imported-ear-file-and-run-server-side-with-arquill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!