在测试工程下编写参数文件是个比较让我头疼的问题。
目前在做的线下渠道运营平台的项目,配置文件老是出错: 一开始我是这么做的,因为开发同学也在做service层的单元测试,他们也会写很多配置文件, 而我们是在自己的测试工程下写,于是我就把开发同学的参数文件给copy过来了。 心想只要他们的单元测试能跑通,我们的测试也应该能跑通。
但我后来发现情况并不是这样,随着项目的开展,开发同学的配置文件随时都在更新, 我之前copy过来的一些配置文件就会过时,而我又不知道开发同学修改了哪些配置文件, 于是一天一个错误,叫这个人看了叫那个人看,修修补补的过了几周,效率实在是低(我才发现我这么有耐心忍呢!),后来是发现了一个问题,别人都搞不定了,以为是框架出了问题,终于把叶渡大侠搬来了。
他在经过仔细的检查之后,给我指出了我的缺点: 开发的配置文件随时都在改,我目前的做法很难跟开发保持同步,该怎么解决呢? 他给了我一个很好的解决方法:直接引用开发的配置文件,例如,在applicationContext-test.xml文件中这样写:
<import resource="classpath:point-core-dao.xml" /> <import resource="classpath:point-core-manager.xml" /> <import resource="classpath:point/service/bean/point-service-bean.xml" /> <import resource="classpath:point/service/bean/channel-common-manager |
以上的几个配置文件,都可以与开发的配置文件保持同步。但还有点细节:开发同学的配置文件中是用变量的,但我们的测试工程中不能使用变量。 例如开发同学的某个bean是这样写的
<bean id=”ctUserManager” > <property name=”channelProfileTargetId” value=”${channel.profile.targetid}”/> <property name=”channelProfileTargetIdByChannel” value=”${channel.profile.targetid.channel}”/> <property name=”channelProfileTargetIdByPoint” value=”${channel.profile.targetid.point}”/> <property name=”channelProfileAppId” value=”${channel.profile.appid}”/> </bean> |
我们引用过来后怎么处理呢?这样,把每个变量定义在另一个文件中,如 dev.properties
channel.profile.targetid = 1053 channel.profile.targetid.channel = 1504 channel.profile.targetid.point = 1505 channel.profile.appid = 2 |
然后在中加入这样的一个bean,其中的value属性的值设置成我们刚才创建的那个变量的定义文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:dev.properties</value> </list> </property> </bean> |
呵呵,问题解决了。