【发布时间】:2019-11-04 06:53:56
【问题描述】:
我在网上尝试了无数解决方案后,近一周来遇到以下困难。具体问题与在我的 JUnit 测试中调用方法 Configuration.getUnmarshallerFactory().getUnmarshaller(Element) 后抛出 NullPointerException 有关。
以下是我的项目中导入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);
}
如果你们之前遇到过类似的错误,我将非常感谢任何帮助或知识分享。
【问题讨论】:
标签: java mockito junit5 powermockito opensaml