0 Replies Latest reply on Jun 23, 2015 8:48 AM by Nikolai Kovalenko

    NullPointerExceptions throws while Arquillian and PowerMockito test runs

    Nikolai Kovalenko Newbie

      Good day. I have a problem with using Arquillian with PowerMock. I wanna test jax-rs service, but I get two NPEs when test starts. NullPointerException throws when PowerMockRule field defined. My test: 

      @RunWith(Arquillian.class)

      @PowerMockIgnore({"javax.management.*", "javax.xml.parsers.*",

          "com.sun.org.apache.xerces.internal.jaxp.*", "ch.qos.logback.*", "org.slf4j.*"})

      public class ResourceRequestServiceIsAvailableIT {

       

          private static final Logger LOGGER = LoggerFactory

                                                     .getLogger(ResourceRequestServiceIsAvailableIT.class);

       

          @Rule

          public PowerMockRule        rule   = new PowerMockRule();

       

       

          @Deployment(testable = true)

          public static Archive<?> createDeployment() {

              final WebArchive war = ShrinkWrap

                      .create(WebArchive.class, "resourceRequestIsAvailableTest.war")

                      // .addAsResource("persistence-test.xml", "META-INF/persistence.xml")

                      .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")

                      .addAsWebInfResource("wildfly-ds.xml")

                      .addAsResource("functionalId.properties")

                      .addAsResource("logback-test.xml", "logback.xml")

                      .addAsResource("xml/organizationalUnit.xml")

                      .addAsResource("xsd/organizationalUnit.xsd")

                      .addAsResource("xml/resourceRequest.xml")

                      .addAsResource("xsd/resourceRequest.xsd")

                      .addClass(ResourceRequestRestService.class)

                      .addPackages(true, "by.iba.gomel.domain")

                      .addPackage("by.iba.gomel.restws.logging")

                      .addPackages(true, "org.apache.commons.io")

                      .addAsLibraries(

                              Maven.resolver().resolve("org.kie.remote:kie-remote-client:6.2.0.Final")

                              .withTransitivity().asFile())

                      .addAsLibraries(

                              Maven.resolver().resolve("org.aspectj:aspectjrt:1.8.5").withTransitivity()

                                      .asFile());

              ResourceRequestServiceIsAvailableIT.LOGGER.info("generated .war file - "

                      + war.toString(true) + "\n");

              return war;

          }

       

          @Test

          public void testStartResourceRequest() {

              final ResteasyClient client = new ResteasyClientBuilder().build();

              final ResteasyWebTarget target = client.target("http" + "://" + "localhost" + ":" + "8080"

                      + "/resourceRequestIsAvailableTest" + "/rest/resourcerequest/startResourceRequest");

              final Form form = new Form();

              try {

                  form.param("rr", IOUtils.toString(ResourceRequestServiceIsAvailableIT.class

                          .getResourceAsStream("/xml/resourceRequest.xml")));

              } catch (final IOException e) {

                  ResourceRequestServiceIsAvailableIT.LOGGER.error(

                          MarkerFactory.getMarker(LoggerConstants.EXCEPTION_LOG_MARKER.getString()),

                          "Problem reading ResourceRequest XML", e);

              }

              try {

                  form.param("ou", IOUtils.toString(ResourceRequestServiceIsAvailableIT.class

                          .getResourceAsStream("/xml/organizationalUnit.xml")));

              } catch (final IOException e) {

                  ResourceRequestServiceIsAvailableIT.LOGGER.error(

                          MarkerFactory.getMarker(LoggerConstants.EXCEPTION_LOG_MARKER.getString()),

                          "Problem reading OrganizationalUnit XML", e);

              }

              final Response response = target.request().post(Entity.form(form));

              ResourceRequestServiceIsAvailableIT.LOGGER.info(

                      "Request successfully sent, response is '{}'", response.getStatusInfo());

              response.close();

          }

      }

      My service method:

      @POST

          @Path("/startResourceRequest")

          @Consumes(MediaType.APPLICATION_FORM_URLENCODED)

          public Response postAll(final MultivaluedMap<String, String> form) {

       

              final URL baseUrl;

              ResourceRequest resourceRequest = null;

              try {

                  final JAXBContext jaxbContext = JAXBContext.newInstance(ResourceRequest.class,

                          OrganizationalUnit.class);

                  final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

                  resourceRequest = (ResourceRequest) unmarshaller.unmarshal(new StringReader(form

                          .getFirst("rr")));

                  baseUrl = new URL("http://localhost:8084/kie-wb");

              } catch (final JAXBException | MalformedURLException e) {

                  ResourceRequestRestService.LOGGER.error(

                          MarkerFactory.getMarker(LoggerConstants.EXCEPTION_LOG_MARKER.getString()),

                          "System problem, check the log", e);

                  return Response.status(Status.INTERNAL_SERVER_ERROR)

                          .entity("System problem, check the log").build();

              }

       

              if (form.containsKey("userId")) {

                  this.user = form.getFirst("userId");

                  this.password = form.getFirst("password");

              }

              if ((this.user == null) || this.user.isEmpty()) {

                  ResourceRequestRestService.LOGGER.error(

                          MarkerFactory.getMarker(LoggerConstants.EXCEPTION_LOG_MARKER.getString()),

                          "Failed to provide authentication data");

                  return Response.status(Status.INTERNAL_SERVER_ERROR)

                          .entity("Failed to provide authentication data").build();

              }

       

              final RuntimeEngine engine = RemoteRuntimeEngineFactory.newRestBuilder()

                      .addDeploymentId(ResourceRequestRestService.DEPLOYMENT_ID).addUrl(baseUrl)

                      .addUserName(this.user).addPassword(this.password)

                      .addExtraJaxbClasses(ResourceRequest.class).build();

              final KieSession ksession = engine.getKieSession();

              final Map<String, Object> processParams = new HashMap<>();

              processParams.put("resourceRequest", resourceRequest);

              ksession.startProcess("Interview.ResourceRequest", processParams);

       

              return Response.status(Status.OK).entity("Business process 'Resource Request' started")

                      .build();

          }

       

      Stacktrace:

      java.lang.NullPointerException: null

          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 java.lang.reflect.Method.invoke(Method.java:606)

          at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)

          at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)

          at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)

          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.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)

          at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)

          at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)

          at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)

          at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)

          at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)

          at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

       

       

      java.lang.NullPointerException: null

          at org.jboss.arquillian.junit.Arquillian$5$1.evaluate(Arquillian.java:245)

          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 java.lang.reflect.Method.invoke(Method.java:606)

          at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)

          at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)

          at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:638)

          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.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)

          at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)

          at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)

          at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)

          at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)

          at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)

          at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

      Please, help to find solution. May be some body knows what's wrong.