JUnit测试:无法解组元素

在尝试了无数网络解决方案后,近一个星期以来,我遇到了以下困难。具体问题与在我的JUnit测试中调用方法NullPointerException之后抛出Configuration.getUnmarshallerFactory().getUnmarshaller(Element)

以下是导入到我的项目中的opensaml库的依赖项信息:

<dependency>
    <groupId>org.opensaml</groupId>
    <artifactId>opensaml</artifactId>
    <version>2.6.4</version>
</dependency>

以下是实现。在正常执行程序/项目期间,它可以成功执行并返回Response对象。

private Response a(String text) throws ConfigurationException,SAXException  {

        DefaultBootstrap.bootstrap();

        Schema s = SAMLSchemaBuilder.getSAML11Schema();

        BasicParserPool bpp = new BasicParserPool();
        bpp.setNamespaceAware(true);
        bpp.setIgnoreElementContentWhitespace(true);
        bpp.setSchema(schema);

        InputStream is= new ByteArrayInputStream(Base64.decode(samlContent).getBytes());
        Response res= null;

        try {
            Document doc = bpp.parse(is);
            Element elmt= doc.getDocumentElement();
            try {

                QName qn = new QName(elmt.getNamespaceURI(),elmt.getLocalName(),elmt.getPrefix());
                Unmarshaller um = Configuration.getUnmarshallerFactory().getUnmarshaller(qn);        <== NullPointerException thrown at this line during JUnit Test**
                samlResponse = (Response) unmarshaller.unmarshall(elmt);
            } catch (XMLParserException e) {
                  logger.debug(e.getMessage());
        } catch (UnmarshallingException e) {
            logger.debug(e.getMessage());
        }

        return res;
    }

以下是JUnit测试: (我从以下网站获得了示例samlp:Response字符串:https://www.samltool.com/generic_sso_res.php

@Test
public void test() throws Exception {
    PowerMockito.mockStatic(DefaultBootstrap.class);       
    PowerMockito.doNothing().when(DefaultBootstrap.class,"bootstrap");


    Response result = classInstance.a(Base64.encode(responseStringFromWebsite));

    assertNotNull(result);
}

如果您之前遇到过类似的错误,我将非常感谢您提供的帮助或知识共享。

oneDollarOne 回答:JUnit测试:无法解组元素

通过模拟方法DefaultBootstrap#bootstrap,我想您已经跳过了必填字段的初始化。检查DefaultBootstrap.bootstrap()的源代码,这将阐明NPE的原因。

本文链接:https://www.f2er.com/3168624.html

大家都在问