Search code examples
junitjboss-arquillianpayara

Arquillian + mvn clean package instead of mvn clean package -DskipTests and mvn test


I am learning Arquillian and I have an issue.

My test only works if at first I generate war with mvn clean package -DskipTests and then execute tests with mvn test command.

If I execute mvn clean package then I get an exception and my tests are not executed:

java.lang.RuntimeException: Could not invoke deployment method: public static org.jboss.shrinkwrap.api.spec.WebArchive a.b.c.HelloBeanTest.createDeployment()
    at a.b.c.HelloBeanTest.createDeployment(HelloBeanTest.java:32)

It would be great if I can execute my tests directly without at first generating the final artifact.

This is my test class:

@RunWith(Arquillian.class)
public class HelloBeanTest {
    @EJB
    private HelloBean bean;

    @Deployment
    public static WebArchive createDeployment() {
        WebArchive war = ShrinkWrap.createFromZipFile(
                WebArchive.class, new File("target/arquillian-demo-web-1.0.war")
        );

        System.out.println(war.toString(true));
        return war;
    }

    @Test
    public void testSay() throws Exception {
        assertNotNull(bean);
        System.out.println(bean.say());
        System.out.println("- end -");
    }

}

I tried the wollowing but it does not work for me:

war = ShrinkWrap.create(MavenImporter.class).loadPomFromFile("pom.xml").importBuildOutput().as(WebArchive.class);

Solution

  • Arquillian provides a better way to do what you want. The best practice is to use ShrinkWrap maven importer to build and package the application instead of maven.

    But before doing it, think twice whether you really want to test whole application (in an integration or system test), or you want to unit test smaller parts or bigger components of the applications. The best practice is to always package only the smallest subset of the whole application that is required to run the test and nothing more (it is then easier to understand what the test is testing and also runs faster).

    If you really want to test the whole application in a single test case, then the ShrinkWrap maven importer I mentioned above should help:

    WebArchive war = ShrinkWrap.create(MavenImporter.class) 
        .loadPomFromFile("pom.xml").importBuildOutput().as(WebArchive.class);