Arquillian:部署jar并对其运行测试

时间:2016-09-29 16:26:54

标签: java jboss-arquillian

我有这样的Arquillian测试:

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

我看到Arquillian创建了war装饰器并尝试将其部署到服务器:

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

有什么问题?为什么它立即被取消部署?

2 个答案:

答案 0 :(得分:0)

您尚未向Jar添加任何资源。还有ejb。 DateService是您创建并想要测试的EJB吗?它是否在您运行测试的项目中? Arquillian要求您将所有必要的资源添加到已部署的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);
}

答案 1 :(得分:0)

可以使用 org.jboss.shrinkwrap.api.ShrinkWrap#createFromZipFile API 从现存的 jar 构建 JavaArchive。如果您的 ejb.jar 文件位于资源根目录中,那么它应该可以通过多种方法解析。类似的东西,

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