maven测试工程,中文乱码问题解决

作者:

写接口测试用例,用到junit,log4j。项目的默认编码是utf-8,text file encoding也是utf-8。

 

单个测试类,通过JUint test方式运行,没有问题,log里面的中午也都正常显示。

16:35:54 [INFO] reqJson={"auth":{"appName":"test","password":"000000","userName":"nocon","mapType":"google"},"cmd":"firstPageData","params":{"objId":"","cityId":"","userId":"74841c603daf4d2ab358542ac3535057","cityName":"南京市"}} 16:35:54 [INFO] resJson={"cmd":"firstPageData","result":0,"resultNote":"Success","totalRecordNum":1,"pages":1,"pageNo":0,"detail":{"getVehicleRest":{"restLicense":"不限行","restNote":""},"getWeatherAndWashIndex":{"city":"南京","today":{"temp":"20~11℃","weather":"多云转阴","wind":"东南风3-4级","washIndex":"适宜","img":"1"},"tomorrow":{"temp":"20~10℃","weather":"多云转晴","wind":"西风3-4级","img":"2"},"afterTomorrow":{"temp":"21~12℃","weather":"晴转多云","wind":"东风转东南风3-4级","img":"1"}}}}

 

可通过Maven build方式运行整个测试工程的时候,测试用例总是失败!!打开log文件,发现里面的中文全是乱码。

16:37:36 [INFO] reqJson={"auth":{"appName":"test","password":"000000","userName":"nocon","mapType":"google"},"cmd":"firstPageData","params":{"objId":"","cityId":"","userId":"74841c603daf4d2ab358542ac3535057","cityName":"�Ͼ���"}} 16:37:36 [INFO] resJson={"cmd":"firstPageData","result":1,"resultNote":"��ݳ������δ��ѯ����Ӧ�ij���ID","totalRecordNum":0,"pages":1,"pageNo":0,"detail":{}}

 

从而推断是maven运行的时候默认编码问题导致此问题。

 

于是首先把maven的sourceEncoding配置为utf-8

UTF-8 UTF-8

 运行之后,仍然报错,中文乱码。

 

然后把maven-compiler-plugin插件中的encoding也变为utf-8

org.apache.maven.plugins maven-compiler-plugin 1.6 1.6 UTF-8

  运行之后,仍然报错,中文乱码。

 

接着继续尝试,把run configurations中的maven build也设置为utf-8

 可,仍然中文乱码报错!!

 

最后,冷静下来。仔细分析,我的工程是测试工程,里面的类都是测试类,maven-compiler-plugin是compiler代码用的插件,而我的maven build的时候,默认是test。

 

 

于是把关注点放在了maven-surefire-plugin插件上,修改如下

org.apache.maven.plugins maven-surefire-plugin 2.5 once -Dfile.encoding=UTF-8 **/*Test.java

 

运行,一片绿色,成功!!

 

走了不少弯路,还是自己对maven的插件不太熟悉造成的。知错能改,一步步来。

 

在此,分享几篇maven插件文章:

https://github.com/nutzam/nutz/commit/5c56b58b848ad83e07b920490d98f567873977fb#diff-600376dffeb79835ede4a0b285078036R157

 

http://hi.baidu.com/danghj/item/e134441e123d22f587ad4ec1

 

the end

免责声明:本文内容均来自互联网,但不代表简明教程的观点和立场.

已有 3097 次赞