others - Java - JUnit测试:Unable to unmarshall element(无法解组元素)

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测试:


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


}



时间:

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

...