Springtest与junit结合测试注解失败原因

问题描述:我利用Springtest与junit结合测试,出现bean注解失败,test测试代码如下:

[html]  view plain  copy
  1. package org.sparkant.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import org.sparkant.common.object.PackageData;  
  6. import org.sparkant.service.test.TestService;  
  7. import org.springframework.test.context.ContextConfiguration;  
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  9.   
  10. import javax.annotation.Resource;  
  11.   
  12.   
  13. /**  
  14.  * 测试mybits连接数据  
  15.  * Created by hjs on 15/9/9.  
  16.  */  
  17.   
  18. @RunWith(SpringJUnit4ClassRunner.class)  
  19. @ContextConfiguration(locations = "/applicationContext.xml")  
  20. public class TestMybatis{  
  21.   
  22.     @Resource  
  23.     private TestService testService;  
  24.   
  25.     @Test  
  26.     public void test() throws Exception{  
  27.   
  28.         PackageData packageData = new PackageData();  
  29.         packageData.put("userId", 1);  
  30.         testService.findUser(packageData);  
  31.     }  
  32.   
  33.   
  34. }  
测试的时候testService总是报空,

解决方法:

经过检查原来是没有继承AbstractJUnit4SpringContextTests 或 AbstractTransactionalJUnit4SpringContextTests

对于 AbstractJUnit4springcontextTests 和 AbstractTransactionalJUnit4SpringContextTests 类的选择:

  如果再你的测试类中,需要用到事务管理(比如要在测试结果出来之后回滚测试内容),就可以使用AbstractTransactionalJUnit4SpringTests类。 事务管理的使用方法和正常使用spring事务管理是一样的。再此需要注意的是,如果想要使用声明式事务管理,即使用 AbstractTransactionalJUnitSpringContextTests类,请在applicationContext.xml文件 中加入transactionManager bean:

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

  如果没有添加上述bean,将会抛出NoSuchBeanDefinitionException,指明 No bean named 'transactionManager' is definded.

OK修改后的代码如下

[html]  view plain  copy
  1. package org.sparkant.test;  
  2.   
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import org.sparkant.common.object.PackageData;  
  6. import org.sparkant.service.test.TestService;  
  7. import org.springframework.test.context.ContextConfiguration;  
  8. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  10.   
  11. import javax.annotation.Resource;  
  12.   
  13.   
  14. /**  
  15.  * 测试mybits连接数据  
  16.  * Created by hjs on 15/9/9.  
  17.  */  
  18.   
  19. @RunWith(SpringJUnit4ClassRunner.class)  
  20. @ContextConfiguration(locations = "/applicationContext.xml")  
  21. public class TestMybatis extends AbstractTransactionalJUnit4SpringContextTests{  
  22.   
  23.     @Resource  
  24.     private TestService testService;  
  25.   
  26.     @Test  
  27.     public void test() throws Exception{  
  28.   
  29.         PackageData packageData = new PackageData();  
  30.         packageData.put("userId", 1);  
  31.         testService.findUser(packageData);  
  32.     }  
  33.   
  34.   
  35. }  

ok 希望帮到大家,少走弯路。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值