spring3单元测试遇到的问题小结

今天在做spring的单元测试,其中遇到不少问题,不过,大多都是环境的原因引起的。因为是两个分开的项目,一个作为另一个的支持项目,只要是待开发好后,供另一个项目jar包用的。就因为这样,开发环境引用的jar包不同。

 

1、一开始,就遇上hamcrest没有包含的问题,原来是我的junit版本引起的,我的junit版本中没有包含hamcrest。

 

2、Offending resource: class path resource [xml/applicationContextTest.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: class path resource [xml/datasourceTest.xml]

一看就知道是xml配置文件名字空间的问题,这个问题也花了不少时间。

后来在csdn上,一位仁兄这样说:如果遇到这个问题,就要注意看你的jar包中的META-INF目录下是不是有这两个文件:spring-handlers及spring-schemas!

csdn的路径:http://topic.csdn.net/u/20110913/11/0f07ef84-fe7a-4505-8ef3-7851df0998a4.html

于是我仔细检查一下我的jar,原来,竟然没有把jar包引进来!(org.springframework.transaction-3.0.5.RELEASE.jar)

后面遇到的几个问题,就知道缺包了。一个个引入。

 

3、基本上可以跑起来了,开始抛出:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined 这样的异常。

的确,我的配置中没有transactionManager定义的,因为,多数据源的原因。

在google中搜了一把。

引用:

My understanding is that in the context of unit tests (TransactionalTestExecutionListener ), the code that otherwise looks up the transaction manager is not used (TransactionInterceptor#determineTransactionManager ).

You could try to annotate your test class with @TransactionConfiguration , which accepts a transactionManager attribute. Not the most elegant way, but possibly the best option for the time being.

地址:http://stackoverflow.com/questions/3027511/when-transactionmanager-is-not-named-transactionmanager

在我们要测试的类中引用@TransactionConfiguration,指向自己的事务管理就可以了。

 

 

全部代码如下:

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/xml/applicationContextTest.xml"})
@TransactionConfiguration(transactionManager = "tm_cmail_web")
@Transactional
public class SysModelTest {

	@Resource
	private WebSysModelService modelService;
	
	@Test
	@Transactional
	@Rollback(true)
	public void testCreate(){
		WebSysModel m=new WebSysModel();
		m.setModelMid("HelloDing");
		m.setModelName("ding");
		m.setCreateTime(Calendar.getInstance().getTime());
		m.setModfiyTime(Calendar.getInstance().getTime());
		short s=1;
		m.setModelType(s);
		m.setModelUrlType(s);
		
		modelService.create(m);
	}
	
}
 小记一下,希望能帮到你。

学习的地址: http://287854442.iteye.com/blog/734322
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值