Friday, May 1, 2015

Arquillian throws IllegalArgumentException during packaging with ShrinkWrap

Just come across Arquillian and find it so cool that I am able to perform unit test on front end without the 'Selenium way'. The way the Selenium did was I have to leave my computer alone while the test is running otherwise the test result will not accurate.

Now with this creature, I am worry free. In 'Arquillian way', I'm no longer have the web deploy to the server and have the server up and running before the test run. All I have to do were done in programming, this will include package the content in war file and get it deploy to server. It wasn't an Ant build, it was using ShrinkWrap.

Assuming I have my classes put inside the package org.huahsin68, the first step in unit test was the war packaging. It was done with the piece shows below:
   @Deployment(testable=false)
   public static WebArchive createDeployments() {
      WebArchive war = ShrinkWrap.create(WebArchive.class, "ThePackage.war");
      war.setWebXML(new File(WEBAPP_SRC, "WEB-INF/web.xml"));
      war.addPackage(java.lang.Package.getPackage("org.huahsin"));
      war.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
      war.addAsWebResource(new File(WEBAPP_SRC, "index.xhtml"));

      return war;
   }
Somehow there is an error with that piece when the test were run. Following stacktrace tells the whole story.
java.lang.RuntimeException: Could not invoke deployment method: public static org.jboss.shrinkwrap.api.spec.WebArchive org.huahsin68.IndexBeanTest.createDeployment()
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.invoke(AnnotationDeploymentScenarioGenerator.java:177)
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.generateDeployment(AnnotationDeploymentScenarioGenerator.java:99)
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.generate(AnnotationDeploymentScenarioGenerator.java:62)
 at org.jboss.arquillian.container.test.impl.client.deployment.DeploymentGenerator.generateDeployment(DeploymentGenerator.java:79)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
 at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
 at org.jboss.arquillian.container.test.impl.client.ContainerEventController.execute(ContainerEventController.java:100)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
 at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:75)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
 at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:60)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
 at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:135)
 at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:115)
 at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.beforeClass(EventTestRunnerAdaptor.java:80)
 at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:182)
 at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:314)
 at org.jboss.arquillian.junit.Arquillian.access$100(Arquillian.java:46)
 at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:199)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
 at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:622)
 at org.jboss.arquillian.container.test.impl.client.deployment.AnnotationDeploymentScenarioGenerator.invoke(AnnotationDeploymentScenarioGenerator.java:173)
 ... 50 more
Caused by: java.lang.IllegalArgumentException: Pack must be specified
 at org.jboss.shrinkwrap.impl.base.Validate.notNull(Validate.java:43)
 at org.jboss.shrinkwrap.impl.base.container.ContainerBase.addPackage(ContainerBase.java:1363)
 at org.huahsin68.IndexBeanTest.createDeployment(IndexBeanTest.java:42)
 ... 55 more
There is a lazy way to resolve this issue, just pick any of the class and retrieve the package name from that class. Thus I replace the code:

war.addPackage(java.lang.Package.getPackage("org.huahsin"));

with this:

war.addPackage(anyClass.class.getPackage());

But this is not the clean way because if the class will going to remove in one day and this could lead to a disaster.

Good luck!

1 comment:

doweeez said...

Thank you. I have been searching for this.