org.springframework.beans.factory.BeanCreationException:创建名称为' JobSeekerServiceTest'的注册自动连接依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource);嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[javax.sql.DataSource]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385) 在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82) 在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:251) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229) 在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60) 在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67) 在org.junit.runners.ParentRunner.run(ParentRunner.java:309) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162) 在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource);嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[javax.sql.DataSource]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ......还有26个 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型[javax.sql.DataSource]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1261) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1009) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:600) ......还有28个
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath*:spring-config.xml")
@TransactionConfiguration(defaultRollback=true,transactionManager="txManager")
@Transactional
public class JobSeekerServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
private JobSeekerDao jobseekerDao = new JobSeekerDao();
@Test
public void testGetCandidateForEmail(){
JobSeeker result = new JobSeeker();
JobSeeker expected = new JobSeeker();
expected.setName("Dummy Job seeker 1");expected.setEmail("email@yahoo.com");expected.setCity("City");expected.setPhone("04523122");
result = jobseekerDao.getCandidateForEmail("email@yahoo.com");
assertEquals(result, expected);
}
}
@SuppressWarnings("unchecked")
@Override
public JobSeeker getCandidateForEmail(String email) {
LOGGER.debug("Searching the candidate after an email:"+email);
Session session = getCurrentSession();
List<JobSeeker> candidates = session.createQuery("from JobSeeker where email=?")
.setParameter(0, email)
.list();
if (candidates.size() > 0) {
return candidates.get(0);
} else {
return null;
}
}
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.awinta.hrms"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">${hibernate.temp.use_jdbc_metadata_defaults}</prop>
<prop key="hibernate.connection.pool_size">${hibernate.connection.pool_size}</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
name="txManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
为什么我收到此错误的任何想法?我是Spring,Hibernate,JUnit的新手。总之,我认为我应该这样做。或者我该怎么做这个测试课?
我在某处读到导致问题是扩展AbstractTransactionalJUnit4SpringContextTests,没有它我不会再得到这个错误。
答案 0 :(得分:-1)
异常是让您知道测试类找不到任何合适的JDBC驱动程序。您应该将JDBC驱动程序添加到测试类路径以解决此问题