java如何导入测试类,如何将测试类添加到导入的Ear文件中并使用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.

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?

解决方案

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 importer = Descriptors.importAs(ApplicationDescriptor.class, "test");

ApplicationDescriptor desc = importer.fromStream(node.getAsset().openStream());

String xml = desc.exportAsString();

// remove lib definition

xml = xml.replaceAll(".*", "");

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值