使用junit测试springMVC和mybatis项目出现autowired field

问题描述

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.com.qzinfo.test.example.ExampleDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.qzinfo.example.dao.ExampleDao cn.com.qzinfo.test.example.ExampleDaoTest.exampleDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.qzinfo.example.dao.ExampleDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:333)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)at org.junit.runners.ParentRunner.run(ParentRunner.java:220)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)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)Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.qzinfo.example.dao.ExampleDao cn.com.qzinfo.test.example.ExampleDaoTest.exampleDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.qzinfo.example.dao.ExampleDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)... 26 moreCaused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [cn.com.qzinfo.example.dao.ExampleDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:903)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:772)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:686)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)... 28 morespring配置文件:<?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"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"default-autowire="byName"><!--强制使用CGLIB--><aop:aspectj-autoproxy proxy-target-class="true" /><!-- 数据源mysql --><bean id="mysql-DataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql:///springmvc" /><property name="username" value="root" /><property name="password" value="123456" /></bean> <!-- spring ibatis SqlMapClient --><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation" value="classpath:sqlmap-config.xml" /><property name="dataSource" ref="mysql-DataSource" /></bean><!--事务管理DataSourceTranSactionManager--><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="mysql-DataSource" /></bean><!--启动spring注解功能--><tx:annotation-driven transaction-manager="transactionManager" /><aop:aspectj-autoproxy /></beans>测试类文件:package cn.com.qzinfo.test.example;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.com.qzinfo.example.dao.ExampleDao;import cn.com.qzinfo.example.model.Example;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:applicationContext.xml"}) public class ExampleDaoTest{@Autowiredprivate ExampleDao exampleDao;@Testpublic void test() {Example example =new Example();example.setOperatorId(111);example.setIntRev1(111);exampleDao.insert(example);}}

解决方案

缺少 context:component-scan 扫描你的组件

时间: 2024-09-20 00:28:00

使用junit测试springMVC和mybatis项目出现autowired field的相关文章

junit-搭建SSM框架进行JUnit测试spring和mybatis整合

问题描述 搭建SSM框架进行JUnit测试spring和mybatis整合 log4j:WARN No such property [converssionPattern] in org.apache.log4j.PatternLayout. log4j:WARN No such property [macFileSize] in org.apache.log4j.RollingFileAppender. [org.springframework.beans.factory.xml.XmlBea

使用junit测试springMVC项目出现错

问题描述 测试类:package cn.com.qzinfo.test.example;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.s

spring/mybatis整合,junit测试报BeanCreationException

问题描述 昨天想整合下springmvc和mybatis,用junit写了个测试,测试过程中,mybatis的mapper无法注入,报BeanCreationException,相关信息大家看下,帮小弟解决下 1 错误堆栈信息    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.hft.dal.mapper.AccountMapperTest': Inject

mybatis spring 整合 junit测试。 事务不起作用,不提交。删除无效???

问题描述 mybatis spring 整合 junit测试. 事务不起作用,不提交.删除无效??? applicationContext.xml 中的数据库和 sessionFactory以及事务配置 Xml代码 <bean id="bssDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connec

spring mvc-springmvc+mybatis tomcat加载时出错,JUNIT测试通过

问题描述 springmvc+mybatis tomcat加载时出错,JUNIT测试通过 springmvc+mybatis tomcat加载时出错,JUNIT测试通过,就是tomcat起不来,请各位大神帮帮忙 十月 25, 2015 6:18:41 下午 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplicationInitializer types detected on classpath 十月 25

Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建

版权声明:本文为博主原创文章,转载注明出处http://blog.csdn.net/u013142781 目录(?)[+] 这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境(主要是Maven+MySQL的配置)的猿友可以参考博主以前的一篇文章: http://blog.csdn.net/u013142781/article/details/5030

Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)

这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境(主要是Maven+MySQL的配置)的猿友可以参考博主以前的一篇文章: http://blog.csdn.net/u013142781/article/details/50300233 接下来马上进入项目搭建过程: 1.创建表,并插入数据: CREATE DATABASE `ssi` /*!4010

myeclipse 发布 测试-在项目里添加了junit测试类,发布的时候不想发布出去怎么处理?

问题描述 在项目里添加了junit测试类,发布的时候不想发布出去怎么处理? 添加了源代码包testsrc,和src并列的,测试程序都在这个包下面 使用的是MyEclipse找到项目属性中有一个设置发布的地方,MyEclipse -->deployment assembly ,去掉了testsrc,但是导出的war文件里还是有testsrc里面的程序 能不能再发布的时候不出现测试程序?

springmvc-做一个项目,用springMVC和mybatis,总有个错误,求大神帮忙

问题描述 做一个项目,用springMVC和mybatis,总有个错误,求大神帮忙 com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not c