在 arquillian 测试中使用 shrinkWrap 在部署 jar 中添加包
add packages in deployment jar using shrinkWrap in arquillian test
我正在使用 arquillian 进行单元测试。我正在使用 shrinkWrap 创建部署 jar。但是为此我需要添加我项目中使用的所有包,数量很多。
以下是我的测试文件
@RunWith(Arquillian.class)
public class GreeterTest {
@Deployment
public static JavaArchive createDeployment() throws NamingException {
return ShrinkWrap.create(JavaArchive.class, "test.jar")
.addPackage(ABC.class.getPackage())
.addPackage(EFG.class.getPackage())
.addPackage(HIJ.class.getPackage())
.addPackage(KLM.class.getPackage())
.addPackage(NOP.class.getPackage())
.addPackage(QRS.class.getPackage())
.addPackage(TUV.class.getPackage())
.addPackage(XYZ.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Inject
ABC abc;
@Inject
EFG efg;
@Inject
HIJ hij;
@Inject
KLM klm;
@Inject
NOP nop;
@Test
public void shouldBeAbleToInjectEJBAndInvoke() throws Exception {
abc.getDetail();
}
}
可以看到.addPackage()。我的项目中有数百个包。明显的代码量会大大增加
还有其他办法吗?或者我一定是犯了什么大错
我建议你使用包路径的字符串表示:"com.root.core"等。还有一些方法:
addPackage(String pack)
addPackages(boolean recursive, String... packages)
我想最新的更适合你,因为它为你提供了递归添加包的可能性,从而避免重复包含每个包。例如:
.addPackages(true, "com.root")
您可以使用应用程序的现有 EAR/WAR/JAR
,因为在某些复杂情况下(许多依赖项等)使用 ShrinkWrap
创建 EAR
会很烦人。
@Deployment
方法应该将测试 WAR 嵌入到 EAR 中,并在将存档返回到 Arquillian 运行时之前将模块元素添加到现有的 application.xml
中。
一个@Deployment
方法示例:
...
@Deployment
public static Archive<?> createDeploymentPackage() throws IOException {
final String testWarName = "test.war";
final EnterpriseArchive ear = ShrinkWrap.createFromZipFile(
EnterpriseArchive.class, new File("target/myApp.ear"));
addTestWar(ear, myClassTest.class, testWarName);
...
Arquillian EJB-JAR/EAR testing examples
Arquillian EJB-JAR/EAR testing examples/Github
SO: How to add test classes to an imported ear file and run server side with arquillian?
我正在使用 arquillian 进行单元测试。我正在使用 shrinkWrap 创建部署 jar。但是为此我需要添加我项目中使用的所有包,数量很多。
以下是我的测试文件
@RunWith(Arquillian.class)
public class GreeterTest {
@Deployment
public static JavaArchive createDeployment() throws NamingException {
return ShrinkWrap.create(JavaArchive.class, "test.jar")
.addPackage(ABC.class.getPackage())
.addPackage(EFG.class.getPackage())
.addPackage(HIJ.class.getPackage())
.addPackage(KLM.class.getPackage())
.addPackage(NOP.class.getPackage())
.addPackage(QRS.class.getPackage())
.addPackage(TUV.class.getPackage())
.addPackage(XYZ.class.getPackage())
.addAsResource("test-persistence.xml", "META-INF/persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Inject
ABC abc;
@Inject
EFG efg;
@Inject
HIJ hij;
@Inject
KLM klm;
@Inject
NOP nop;
@Test
public void shouldBeAbleToInjectEJBAndInvoke() throws Exception {
abc.getDetail();
}
}
可以看到.addPackage()。我的项目中有数百个包。明显的代码量会大大增加
还有其他办法吗?或者我一定是犯了什么大错
我建议你使用包路径的字符串表示:"com.root.core"等。还有一些方法:
addPackage(String pack)
addPackages(boolean recursive, String... packages)
我想最新的更适合你,因为它为你提供了递归添加包的可能性,从而避免重复包含每个包。例如:
.addPackages(true, "com.root")
您可以使用应用程序的现有 EAR/WAR/JAR
,因为在某些复杂情况下(许多依赖项等)使用 ShrinkWrap
创建 EAR
会很烦人。
@Deployment
方法应该将测试 WAR 嵌入到 EAR 中,并在将存档返回到 Arquillian 运行时之前将模块元素添加到现有的 application.xml
中。
一个@Deployment
方法示例:
...
@Deployment
public static Archive<?> createDeploymentPackage() throws IOException {
final String testWarName = "test.war";
final EnterpriseArchive ear = ShrinkWrap.createFromZipFile(
EnterpriseArchive.class, new File("target/myApp.ear"));
addTestWar(ear, myClassTest.class, testWarName);
...
Arquillian EJB-JAR/EAR testing examples
Arquillian EJB-JAR/EAR testing examples/Github
SO: How to add test classes to an imported ear file and run server side with arquillian?