当前位置: 首页 > 知识库问答 >
问题:

JUnit/Spring/Hibernate-没有[javax.sql.datasource]类型的限定bean

邬飞捷
2023-03-14
@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的新手,冬眠的,junit的。总之,我认为我应该这样做。或者我该怎么做这个测试类?

我在某处读到导致问题的原因是extends AbstractTransactionalJUnit4SpringContextTest,如果没有它,我就不会再出现这个错误了。

共有1个答案

佟寒
2023-03-14

异常是让您知道测试类找不到任何合适的JDBC驱动程序。您应该向测试类路径添加一个JDBC驱动程序来解决这个问题

 类似资料: