1 Reply Latest reply on Jul 21, 2014 5:32 PM by Feng Ji

    Run test with two runners: Arquillian and Powermock

    Maksim Voloshin Newbie

      Hi everybody!

       

      I have a some problem. I have to use both runner: arquillian and powermock. But @RunWith may be only once time. I try to use annotation @Rule for start Powermock but I'm having an error:

       

      java.lang.NullPointerException
        at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:222)
        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.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:52)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:2014)
        at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:885)
        at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:713)
        at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
        at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:98)
        at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
        at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        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:309)
        at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:147)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
      

       

      My code is:

       

      @RunWith(Arquillian.class)
      @PrepareForTest(SOAPTools.class)
      public class createOrderDocumentTest {
        @Rule
        public PowerMockRule rule = new PowerMockRule();
      
        private createOrderDocument cod;
        private DBLookup dbLookup;
      
        @Deployment
        public static WebArchive createDeployment() {
          WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
              .addClasses(DBLookup.class, createOrderDocument.class, MessageSink.class)
              .addAsResource("hibernate.cfg.xml", "META-INF/persistence.xml")
              .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
          System.out.println(war.toString(true));
          return war;
        }
      
        @Before
        public void init() {
          try {
            dbLookup = mock(DBLookup.class);
            cod = new createOrderDocument();
            Whitebox.setInternalState(cod, "dbLookUp", dbLookup);
          } catch (Exception e) {}
        }
      
        @Test
        public void createOrderDocumentMethodTest() {
          XMLBranch xmlBranch = new XMLBranch(getDocumentFromXML());
          try {
            MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage(new MimeHeaders(), new ByteArrayInputStream(xmlRequest.getBytes()));
            mockStatic(SOAPTools.class);
            doNothing().when(SOAPTools.class, "EJBcall", eq("java:global/some/ejb/name"), anyObject());
            cod.processMessage(xmlBranch);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      

       

      When I removed a @Deployment annotation I can't mock a static method.

       

      How can I use a Powermock with arquillian?

       

      Thx for your reply.

        • 1. Re: Run test with two runners: Arquillian and Powermock
          Feng Ji Newbie

          I had the same problem before I switched to use agent based powermock. I use ivy and the following set up works for me.

                   <dependency org="org.powermock" name="powermock-api-mockito" rev="1.5.5" conf="default"/>

                  <dependency org="org.powermock" name="powermock-api-support" rev="1.5.5"  conf="default"/>

                  <dependency org="org.powermock" name="powermock-core" rev="1.5.5" conf="default"/>

                  <dependency org="org.powermock" name="powermock-reflect" rev="1.5.5" conf="default"/>

                  <dependency org="org.powermock" name="powermock-module-junit4" rev="1.5.5" conf="default"/>

                  <dependency org="org.powermock" name="powermock-module-junit4-common" rev="1.5.5" conf="default"/>

                  <dependency org="org.powermock" name="powermock-module-javaagent" rev="1.5.5" conf="default"/>

                  <dependency org="org.powermock" name="powermock-module-junit4-rule-agent" rev="1.5.5" conf="default"/>

          For more information regarding power mock agent see:

          https://code.google.com/p/powermock/wiki/PowerMockAgent