No bean named transactionManager is defined 多数据源的单元测试

spring 相关的配置


	
		
			${jdbc.driverClassName}
		
		
			${jdbc.url}
		
		
			${jdbc.username}
		
		
			${jdbc.password}
		
		
		
		
		
		
		
		
		
		
		
			
				true
				GBK
				GBK
			
		
	
	
		
			${jdbc.driverClassName}
		
		
			${jdbc.url}
		
		
			${jdbc.username}
		
		
			${jdbc.password}
		
		
		
		
		
		
		
		
		
		
		
			
				true
				GBK
				GBK
			
		
	

	
			
		

		
			
				
				
				
				
				
				
			
		
		
		
			
		

		
			
				
				
				
				
				
				
			
		

 单元测试类:

@ContextConfiguration(locations = {"classpath:spring/common/dataSource.xml","classpath:spring/common/applicationContext.xml",
		"classpath:spring/car/car.xml","classpath:spring/car/car-dao.xml",
		"classpath:spring/biap/biap.xml","classpath:spring/biap/biap-dao.xml"})
public abstract class BaseTest extends AbstractTransactionalJUnit4SpringContextTests {
       //自动装配默认是根据类型类匹配的,用@Qualifier 可以指定具体的数据源
       @Autowired
	public void setDataSource(@Qualifier("car_dataSource") DataSource dataSource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

}
public class BasedaoTest extends BaseTest{
	@Resource(name="carBaseDAO")
	BaseDAO carBaseDAO;
	private String namespace="car_type.";
	@Test
	public void batchDelete() {
		List list=new ArrayList();
		for (int i=2;i<10;i++) {
			list.add(i);
		}
		this.carBaseDAO.batchDelete(namespace, "deleteCarType", list);
	}
} 

 运行之后报错

[WARN ] 2012-07-06 10:58:20 :Caught exception while retrieving transaction manager with bean name [transactionManager] for test context [[TestContext@d12eea testClass = BasedaoTest, locations = array['classpath:spring/common/dataSource.xml', 'classpath:spring/common/applicationContext.xml', 'classpath:spring/car/car.xml', 'classpath:spring/car/car-dao.xml', 'classpath:spring/biap/biap.xml', 'classpath:spring/biap/biap-dao.xml'], testInstance = com.hhb.car.dao.test.BasedaoTest@15f157b, testMethod = batchDelete@BasedaoTest, testException = [null]]]
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:884)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:298)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:146)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:292)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runBefores(SpringMethodRoadie.java:270)
	at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:332)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
	at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
[WARN ] 2012-07-06 10:58:20 :Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@b6ef8] to process 'before' execution of test method [public void com.hhb.car.dao.test.BasedaoTest.batchDelete()] for test instance [com.hhb.car.dao.test.BasedaoTest@15f157b]
 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:387)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:971)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:246)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:168)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:884)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.getTransactionManager(TransactionalTestExecutionListener.java:298)
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:146)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:292)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runBefores(SpringMethodRoadie.java:270)
	at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:332)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
	at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
	at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

 经过断点跟踪调试:

 

org.springframework.test.context.transaction.TransactionalTestExecutionListener
private TransactionConfigurationAttributes retrieveTransactionConfigurationAttributes(Class clazz) {
		Class annotationType = TransactionConfiguration.class;
		TransactionConfiguration config = clazz.getAnnotation(annotationType);
		if (logger.isDebugEnabled()) {
			logger.debug("Retrieved @TransactionConfiguration [" + config + "] for test class [" + clazz + "]");
		}

		String transactionManagerName;
		boolean defaultRollback;
		if (config != null) {
			transactionManagerName = config.transactionManager();
			defaultRollback = config.defaultRollback();
		}
		else {
			transactionManagerName = (String) AnnotationUtils.getDefaultValue(annotationType, "transactionManager");
			defaultRollback = (Boolean) AnnotationUtils.getDefaultValue(annotationType, "defaultRollback");
		}

		TransactionConfigurationAttributes configAttributes =
				new TransactionConfigurationAttributes(transactionManagerName, defaultRollback);
		if (logger.isDebugEnabled()) {
			logger.debug("Retrieved TransactionConfigurationAttributes [" + configAttributes + "] for class [" + clazz + "]");
		}
		return configAttributes;
	}
 
 

 默认它会加上事务,事务管理默认的bean名称是transactionManager,因为配置中没有这个bean 的配置,所以抛出异常,这个        TransactionConfiguration config = clazz.getAnnotation(annotationType);
我们没有在测试类上注释,所以为null,去取默认的transactionManager。

我们可以这样做

@TransactionConfiguration(transactionManager="car_transactionManager")
public class BasedaoTest extends BaseTest{

}

 

 

你可能感兴趣的:(No bean named transactionManager is defined 多数据源的单元测试)