我目前有一个单元测试,它将 XML 文件解组为 Jaxb 对象,然后将其编组为字符串并将其与原始 xml 字符串进行比较:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/info/info-element-reader-test-context.xml"})
public class InfoElementReaderTest {
private static final Logger logger = LoggerFactory.getLogger(InfoElementReaderTest.class);
@Autowired
private InfoElementReader infoElementReader;
@Test
public void testMarshalling() throws Exception {
Marshaller marshaller = infoElementReader.getJaxbContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
// Unmarshall into java bean
InfoElement infoElement = infoElementReader.unmarshallInfoElement();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
marshaller.marshal(infoElement, outStream);
logger.info("marshalled XML: {}", outStream.toString());
String fromFile = IOUtils.toString(infoElementReader.getInfoResource().getInputStream(), "UTF-8").replaceAll("\\s","");
String marshalled = outStream.toString().replaceAll(""", "\"").replaceAll("\\s","");
assertEquals("file contents should equal marshalled output", fromFile, marshalled);
}
}
当我在我的 IDE(Intellij IDEA)中运行此测试时,它似乎没有问题,但是当我使用 mvn clean package 编译时,此单元测试失败,因为 XML 中的所有重音字符,即。Reddetelefoníamóvil,变成 Reddetelefon[?am?vil 和弦显然不相等。
不确定这是否是 maven 或其他问题的问题,但我们将不胜感激。提前致谢。