简体   繁体   中英

Arquillian: Deploy jar and run tests against it

I have such Arquillian test:

@Deployment
public static JavaArchive createDeployment() {
    return ShrinkWrap.create(JavaArchive.class, "ejb.jar");// ejb.jar is in a resource root  
}

@EJB
private DateService dateService;

@Test
public void shouldBeAbleToInjectEJB() throws Exception {
    Assert.assertNotNull(dateService);
}

I see that Arquillian creats war decorator and tries to deploy it to server:

19:18:34,773 WARN  [org.jboss.weld.deployer] (MSC service thread 1-5) JBAS016012: Deployment deployment "test.war" contains CDI annotations but beans.xml was not found.
19:18:34,822 INFO  [org.jboss.web] (ServerService Thread Pool -- 15) JBAS018210: Register web context: /test
19:18:35,010 INFO  [org.jboss.as.server] (management-handler-thread - 2) JBAS018559: Deployed "test.war" (runtime-name : "test.war")
19:18:35,590 INFO  [org.jboss.web] (ServerService Thread Pool -- 15) JBAS018224: Unregister web context: /test
19:18:35,617 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-8) JBAS015877: Stopped deployment test.war (runtime-name: test.war) in 30ms

What is wrong? Why it was undeployed immediately?

You haven't added any resources to the Jar. There is ejb's added. Is DateService an EJB you have created and want to test? Is it in the project within which you are running the test? Arquillian requires you add all the necessary resource to the deployed jar.

@Deployment
public static JavaArchive createDeployment() {
    return ShrinkWrap.create(JavaArchive.class, "ejb.jar").addClass(DateService.class);// ejb.jar is in a resource root  
}

@EJB
private DateService dateService;

@Test
public void shouldBeAbleToInjectEJB() throws Exception {
    Assert.assertNotNull(dateService);
}

It is possible to build a JavaArchive from an extant jar using the org.jboss.shrinkwrap.api.ShrinkWrap#createFromZipFile API. If your ejb.jar file is in the resource root, then it should be resolvable via several methods. Something like,

public static JavaArchive createDeployment() {
    return ShrinkWrap.createFromZipFile(JavaArchive.class, classpathFile("ejb.jar"));
}

@EJB
private DateService dateService;

@Test
public void shouldBeAbleToInjectEJB() throws Exception {
    Assert.assertNotNull(dateService);
}

private static File classpathFile(String filePathOnClasspath) {
    ClassLoader classLoader = YourTestClass.class.getClassLoader();
    URL resource = classLoader.getResource(filePathOnClasspath);
    URI fileUri = null;
    try {
        fileUri = resource.toURI();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return Paths.get(fileUri).toFile();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM