lemon_cake 2017-09-30 09:39 采纳率: 50%
浏览 2081

junit加载配置文件出错

错误信息如下:

 Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testServiceImpl': Unsatisfied dependency expressed through field 'userInfoDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userInfoDao' defined in file [E:\EclipseWorkspace\mvn-springmvc\target\classes\com\dao\user\UserInfoDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring/applicationContext-dao.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/applicationContext-dao.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory

applicationContext-service.xml配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.3.xsd">


    <!-- 扫描包 controller,service-->
    <context:component-scan base-package="com.serviceimpl"></context:component-scan>

</beans>

applicationContext-dao.xml配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"
    default-lazy-init="false">

    <!-- 加载资源文件 -->
    <context:property-placeholder ignore-resource-not-found="true" location="classpath:conf/*.properties"/>

    <!-- 使用spring 加载jdbc驱动 -->
    <bean id="dataSourceTemplate"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${uname}" />
        <property name="password" value="${password}" />
    </bean>

    <!-- 数据连接池 -->
    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close">
        <property name="poolProperties">
            <bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
                <!-- 通过引用加载数据库信息 -->
                <property name="dataSource" ref="dataSourceTemplate" />
                <property name="jmxEnabled" value="true" />
                <property name="testWhileIdle" value="true" />
                <property name="testOnBorrow" value="true" />
                <property name="testOnReturn" value="false" />
                <property name="validationInterval" value="30000" />
                <property name="validationQuery" value="SELECT 1" />
                <property name="timeBetweenEvictionRunsMillis" value="30000" />
                <property name="logAbandoned" value="false" />
                <property name="removeAbandoned" value="true" />
                <property name="removeAbandonedTimeout" value="60" />
                <!-- 初始化连接大小 -->
                <property name="initialSize" value="10"></property>
                <!-- 连接池最大数量 -->
                <property name="maxActive" value="200"></property>
                <!-- 连接池最小空闲 -->
                <property name="minIdle" value="10"></property>
                <!-- 获取连接最大等待时间 -->
                <property name="maxWait" value="60000"></property>
                <property name="jdbcInterceptors"
                    value="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" />
            </bean>
        </property>
    </bean>

    <!-- MyBatis配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="typeAliasesPackage" value="com.model" />
        <!-- mybatis位置 -->
        <property name="configLocation" value="classpath:/mybatis/mybatis.xml" />
        <!-- 显式指定Mapper文件位置 -->
        <property name="mapperLocations" value="classpath:/mybatis/**/*Mapper.xml" />
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao" />
    </bean>

    <!-- 配置Spring的事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 注解方式配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

测试基类:

 package test.service;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试基类
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext-service.xml","classpath:/spring/applicationContext-dao.xml","classpath:/spring/applicationContext-redis.xml"})
public class BaseTest {
    // "classpath:/spring/applicationContext-redis.xml"
    // extends AbstractTransactionalJUnit4SpringContextTests 需要回滚时继承
}

测试类

 package test.service.test;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.service.test.TestService;

import test.service.BaseTest;

public class TestServiceTest extends BaseTest {

    @Autowired
    private TestService testService;

    @Test
    public void test() {
        System.out.println(testService.getUserInfo().getUserName());
    }
}

说明:项目能正常启动,正常运行。但是juint测试的时候就出问题了。
到现在都没找出原因。欲哭无泪啊!求帮助

  • 写回答

1条回答 默认 最新

  • devmiao 2017-10-01 00:48
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL