在Arquillian和PowerMockito测试运行时抛出NullPointerExceptions

时间:2015-06-23 11:25:36

标签: java rest junit powermock jboss-arquillian

美好的一天。使用Arquillian和PowerMock时遇到问题。 我想测试jax-rs服务,但是在测试开始时我得到了两个NPE。 定义PowerMockRule字段时抛出NullPointerException。 我的测试:

@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")
            .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();
}
}

我的服务方式:

@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();
}

堆栈跟踪:

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

请帮助找到解决方案。可能是某些人知道什么是错的。

1 个答案:

答案 0 :(得分:0)

您的问题似乎与Jboss论坛here中的帖子非常相似。 请检查一下。可能有帮助。

此致 人士Himanshu