我目前有一个单元测试,它将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或其他什么问题,但任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:0)
您是否在pom文件中设置了编码? UTF-8