重要代码收集之Spring项目单元测试必要代码

本文介绍在Spring开发中如何进行单元测试的初始化设置,包括加载日志配置文件和Spring配置文件的方法,并展示了通过Bean ID获取对象的过程及依赖的jar包。

1. spring 开发中单元测试时的初始化参数(加载日志配置文件、加载 spring 配置文件)

  • 代码片段
DOMConfigurator.configure ("./files/log4j.xml");  // 加载日志文件
		
ApplicationContext contextConfig = new FileSystemXmlApplicationContext("./files/base_beans.spring.xml"); // 加载 spring 文件

PrototypeEntity prototype = contextConfig.getBean("prototypeEntity", PrototypeEntity.class); // 通过 bean id 获取对象


// bean 的注入、scope="prototype"  表示多例
<bean id="prototypeEntity" scope="prototype" class="com.aiden.demo.base.entites.PrototypeEntity">
	<property name="testService" ref="testService"/>
</bean>
		
  • 依赖 jar 包

  • 测试案例截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值