1 Reply Latest reply on Aug 31, 2012 6:11 AM by Aslak Knutsen

    Unit testing with Arquillian, Junit and JMockIt

    Massimo Manfredino Newbie

      Hello to everyone,

      we are trying to setup a unit testing scenario using Arquillian along with JMockIt for mocking the actual code of the method of the class under test.

      The relevant part of our pom looks like this

       

      ...

      <dependencyManagement>

                          <dependencies>

                                    <dependency>

                                              <groupId>org.jboss.shrinkwrap.resolver</groupId>

                                              <artifactId>shrinkwrap-resolver-bom</artifactId>

                                              <version>2.0.0-alpha-1</version>

                                              <scope>import</scope>

                                              <type>pom</type>

                                    </dependency>

                                    <dependency>

                                              <groupId>org.jboss.arquillian</groupId>

                                              <artifactId>arquillian-bom</artifactId>

                                              <version>1.0.2.Final</version>

                                              <scope>import</scope>

                                              <type>pom</type>

                                    </dependency>

                          </dependencies>

                </dependencyManagement>

       

                <dependencies>

                   <!-- COMPILE DEPENDENCIES -->

                   <dependency>

                  <groupId>log4j</groupId>

                  <artifactId>log4j</artifactId>

                  <version>1.2.16</version>

              </dependency>

             

                   <!-- PROVIDED DEPENDENCIES -->

              <dependency>

                  <groupId>javax</groupId>

                  <artifactId>javaee-api</artifactId>

                  <version>6.0</version>

                  <scope>provided</scope>

              </dependency>

             

              <dependency>

                  <groupId>org.jboss.spec</groupId>

                  <artifactId>jboss-javaee-6.0</artifactId>

                  <!-- <version>2.0.0.Final</version> -->

                  <version>1.0.0.Final</version>

                  <type>pom</type>

                  <scope>provided</scope>

              </dependency>

       

              <!-- TEST DEPENDENCIES -->

              <!-- PUT JMOCKIT DEPENDENCY BEFORE JUNIT OTHERWISE THE FOLLOWING ERROR

                  IS RAISED

                  WARNING: JMockit was initialized on demand, which may cause certain

                  tests to fail; please check the documentation for better ways to get

                  it initialized.

               -->

              <dependency>

                  <groupId>com.googlecode.jmockit</groupId>

                  <artifactId>jmockit</artifactId>

                  <version>0.999.16</version>

                  <scope>test</scope>

              </dependency>

             

              <dependency>

                  <groupId>junit</groupId>

                  <artifactId>junit</artifactId>

                  <version>4.10</version>

                  <scope>test</scope>

              </dependency>

             

                          <!-- ARQUILLIAN TEST DEPENDENCIES -->

                          <dependency>

                                    <groupId>org.jboss.arquillian.junit</groupId>

                                    <artifactId>arquillian-junit-container</artifactId>

                                    <scope>test</scope>

                          </dependency>

                          <!-- <dependency>

                  <groupId>org.jboss.arquillian.container</groupId>

                  <artifactId>arquillian-container-test-api</artifactId>

              </dependency> -->

       

                          <!-- And some additional ShrinkWrap descriptors that let us define or load

                  and modify beans.xml, persistence.xml, etc at test time -->

              <dependency>

                  <groupId>org.jboss.shrinkwrap.descriptors</groupId>

                  <artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>

                  <scope>test</scope>

              </dependency>

              <!-- Add ShrinkWrap support for resolution of maven artifacts and addition

                  of dependencies into the test WAR archive using Maven. -->

              <dependency>

                  <groupId>org.jboss.shrinkwrap.resolver</groupId>

                  <artifactId>shrinkwrap-resolver-impl-maven</artifactId>

                  <scope>test</scope>

              </dependency>

             

                          <!-- ARQUILLIAN JBOSS SPECIFIC TEST DEPENDENCIES -->

                          <dependency>

                  <groupId>org.jboss.arquillian.protocol</groupId>

                  <artifactId>arquillian-protocol-servlet</artifactId>

                  <scope>test</scope>

              </dependency>

       

                          <dependency>

                  <groupId>org.jboss.as</groupId>

                  <artifactId>jboss-as-arquillian-container-remote</artifactId>

                  <version>7.1.1.Final</version>

                  <scope>test</scope>

              </dependency>

      ...

       

      while in out test class we create a jar to be deployed with

       

      @Deployment

          public static JavaArchive createDeployment() {

      //                    create jar

                          JavaArchive greeterCDIJar = ShrinkWrap.create(JavaArchive.class, "greeter-cdi.jar");

       

                          greeterCDIJar.addClasses(Greeter.class, PhraseBuilder.class);

                          greeterCDIJar.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

       

                          System.out.println("Composizione greeter-cdi.jar");

              System.out.println(greeterCDIJar.toString(true));

             

              return greeterCDIJar;

          }

       

      (the classes above are just the ones proposed by the Arquillian CDI tutorial)

       

      and try to execute a test method (which involves some mocking) like this

       

          @Test

          public void dummyTest() {

                    new MockUp<PhraseBuilder>() {

                                   @SuppressWarnings("unused")

                @Mock

                public String getFoo() {

                      return new String("bar");

                }

          };

         

              .... (some assertion here)

          }

       

      please note how the actual method getFoo of the PhraseBuilder class gets mocked here!

       

      Under these conditions we got the following exceptio

       

      java.lang.ExceptionInInitializerError

                at org.arquillian.example.GreeterTest2.dummyTest(GreeterTest2.java:93)

                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:601)

                at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)

                at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

                at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

                at org.jboss.arquillian.junit.Arquillian$6$1.invoke(Arquillian.java:270)

                at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.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:601)

                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.execution.ContainerTestExecuter.execute(ContainerTestExecuter.java:38)

                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:601)

                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.createTestContext(TestContextHandler.java:89)

                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:601)

                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.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:601)

                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:601)

                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.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:111)

                at org.jboss.arquillian.junit.Arquillian$6.evaluate(Arquillian.java:263)

                at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:226)

                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$5.evaluate(Arquillian.java:240)

                at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

                at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)

                at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)

                at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

                at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

                at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

                at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

                at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

                at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:185)

                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:300)

                at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)

                at org.junit.runner.JUnitCore.run(JUnitCore.java:157)

                at org.junit.runner.JUnitCore.run(JUnitCore.java:136)

                at org.jboss.arquillian.junit.container.JUnitTestRunner.execute(JUnitTestRunner.java:65)

                at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.executeTest(ServletTestRunner.java:160)

                at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.execute(ServletTestRunner.java:126)

                at org.jboss.arquillian.protocol.servlet.runner.ServletTestRunner.doGet(ServletTestRunner.java:90)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

                at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

                at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

                at java.lang.Thread.run(Thread.java:722)

      Caused by: java.lang.IllegalArgumentException: URI scheme is not "file"

                at java.io.File.<init>(File.java:395)

                at mockit.internal.startup.AgentInitialization.getPathToJarFileContainingThisClass(AgentInitialization.java:88)

                at mockit.internal.startup.AgentInitialization.discoverPathToJarFile(AgentInitialization.java:39)

                at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:18)

                at mockit.internal.startup.Startup.verifyInitialization(Startup.java:84)

                at mockit.MockUp.<clinit>(MockUp.java:35)

                ... 94 more

       

      The sample maven project we are playing with is attached.

       

      Many thanks.