`
yong3773
  • 浏览: 32516 次
社区版块
存档分类
最新评论

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

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	</properties>

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

 

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

    <plugin> 
              <groupId>org.apache.maven.plugins</groupId> 
              <artifactId>maven-compiler-plugin</artifactId> 
              <configuration> 
                  <source>1.6</source> 
                  <target>1.6</target> 
                  <encoding>UTF-8</encoding>
              </configuration> 
           </plugin>

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

 

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



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

 

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

 

 

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

                     <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<forkMode>once</forkMode>
					<argLine>-Dfile.encoding=UTF-8</argLine>
					<includes>
						<include>**/*Test.java</include>
					</includes>
					<excludes>
					</excludes>
				</configuration>
			</plugin>

 

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

 

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

 

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

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

 

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

 

  • 大小: 103.4 KB
分享到:
评论
1 楼 lucky2touch 2014-06-19  
也遇到了一样的问题,感谢楼主,少走了些弯路

相关推荐

Global site tag (gtag.js) - Google Analytics