1

我正在使用 JBoss EAP 6.1 开发一个 Java EE 应用程序。

使用 Arquillian 运行测试时,我在使用注入测试的方法时总是出现空指针异常,我发现 ShrinkWrap 无法使用我的类创建 war 文件夹。

这是我的 Arquillian 收缩包装,它不会创建 war 文件夹。

@Deployment(name = "Test")
@OverProtocol("Servlet 3.0")
public static Archive<?> createDeployment() {

    WebArchive archive = ShrinkWrap
            .create(WebArchive.class, "test_archive.war")
            .addClass(ArquillianTest.class)
            .addPackages(true, "it.payroll.model")
            .addPackages(true, "it.payroll.dao")
            .addPackages(true, "it.payroll.controller")
            .addAsResource("META-INF/persistence.xml")
            .addAsWebInfResource(EmptyAsset.INSTANCE,
                    ArchivePaths.create("beans.xml"));

    archive.as(ZipExporter.class).exportTo(
            new File("target/test_archive.war"), true);

    return archive;
}

谢谢你的帮助。

4

1 回答 1

0

一个工作文件

@ArquillianSuiteDeployment
public class ArquillianDeploymentHelper {

public static final String DEPLOYMENT_NAME = "test";

private static final Logger LOGGER = LoggerFactory.getLogger(ArquillianDeploymentHelper.class);

private static final String WEBAPP_SRC = "src/main/webapp";
private static final String TEST_RESOURCES_SRC = "src/test/resources";
private static final String POM_FILE = "pom.xml";
private static final String ARCHIVE_NAME = "arquillian-test.war";

@Deployment(name = DEPLOYMENT_NAME)
public static Archive<?> generateDefaultDeployment() {

    // Generate the default WAR used by all *IT tests using @OperateOnDeployment("test") annotation
    LOGGER.info("Generating " + ARCHIVE_NAME + " archive ...");

    PomEquippedResolveStage pom = Maven.resolver().loadPomFromFile(POM_FILE);
    ScopeType[] scopes = {ScopeType.COMPILE, ScopeType.IMPORT, ScopeType.TEST}; // no SYSTEM and no PROVIDED
    File[] libs = pom.importDependencies(scopes).resolve().using(TransitiveStrategy.INSTANCE).asFile();

    WebArchive archive =  ShrinkWrap.create(WebArchive.class, ARCHIVE_NAME)
            .addAsLibraries(libs)
            //.addAsResource(new File(TEST_RESOURCES_SRC, "test-configuration.properties"), "configuration.properties")
            .addPackages(true, "com.test")
            //.addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/web.xml"))
            .addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/beans.xml"))
            .addAsWebInfResource(new File(WEBAPP_SRC, "WEB-INF/jboss-web.xml"))
            .addAsWebInfResource(new File(TEST_RESOURCES_SRC, "persistence.xml"), "classes/META-INF/persistence.xml");
            //.addAsManifestResource(new File(WEBAPP_SRC, "META-INF/jboss-deployment-structure.xml"));

    // No need to log the content anymore, the archive is kept in target directory 
    // "deploymentExportPath" variable in arquillian.xml
    // LOGGER.info(archive.toString(true));

    LOGGER.info(archive.toString(true));

    return archive;
}
}
于 2016-12-12T09:15:49.620 回答