问题
I'm getting started with Arquillian and followed the tutorial. It covers how to inject EJBs into a test case. Since Arquillian is targeting Java Web and EE projects, I'm very suprised that the separation of entity classes and EJB interfaces wasn't covered in the tutorial since at least EE projects where everything is dumped in one project are rare.
Since there's no coverage in the tutorial and no understandable error message by any of the EE containers, I managed to extract a test case by trial and error which shows that classes used in
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(MyXBean.class, DefaultMyXBean.class, TermsOfUse.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
which are on the classpath, but not in the same Maven project the test in contained in, fail due to NoClassDefFoundError
. Moving the class into the project without touching anything else makes the deployment work.
Is there any way to use Arquillian in a "real" Java EE in which entities and interfaces are not contained into the .war
of the test, but as a sibling inside the parent .ear
archive of the .war
?
I could think of adding JARs from the Maven cache with hardcoded pathes, but that can't be it, can it?
How test in Java ee using Arquillian maven multiple project might be about the same issue.
回答1:
An EAR deployment with libraries can be built like this:
@Deployment
public static Archive<?> createDeploymentPackage() throws IOException {
MavenDependencyResolver resolver = DependencyResolvers
.use(MavenDependencyResolver.class)
.loadMetadataFromPom("test1-pom.xml");
final JavaArchive ejbJar = ShrinkWrap.create(JavaArchive.class, "my-ejbs.jar").addClass(SomeEjb.class);
final WebArchive webApp = ShrinkWrap.create(WebArchive.class, "my-webap.war").addClass(MyServlet.class);
final EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class)
.setApplicationXML("application.xml")
.addAsLibraries(resolver.artifact("log4j:log4j:1.2.17").resolveAsFiles())
.addAsModule(ejbJar)
.addAsModule(webApp);
return ear;
}
来源:https://stackoverflow.com/questions/46516965/how-to-add-classes-from-another-to-the-arquillian-deployment-archive