简体   繁体   English

Spring MVC应用程序的DAO测试时“没有合格的类型的bean”

[英]“ No qualifying bean of type ” while DAO testing of spring mvc application

I have written a spring mvc web application. 我已经编写了一个Spring MVC Web应用程序。 The application runs properly. 该应用程序正常运行。 I am trying to write DAO tests using spring Test suite. 我正在尝试使用spring测试套件编写DAO测试。 I am using the same config file servlet-context.xml as used by the web-app. 我正在使用与Web应用程序相同的配置文件servlet-context.xml。 When I run the test I get No qualifying bean of type error 当我运行测试时,我得到没有类型错误的合格bean

I have looked around stackoverflow but it "appears" my configuration is right. 我环顾了stackoverflow,但“显示”我的配置正确。 Please suggest :- find the code below :- 请建议:-找到以下代码:-

 package net.codejava.spring.dao;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.List;

import junit.framework.Assert;
import net.codejava.spring.model.User;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:*/servlet-context.xml")
public class UserDAOImplTests {

    @Autowired
    public UserDAO userDao;//=new UserDAOImpl();

    @Test
    public void test() {
        User user=new User();
        user.setAddedBy(1);
        user.setCreated(new Date());
        user.setEmail("imtoshi01@gmail.com");
        user.setFirstName("Toshi");
        user.setId(98);
        user.setIsSoftDeleted('N');
        user.setLastName("Singhal");
        user.setLicenseId(1);
        user.setMobile("9030137901");
        user.setPassword("password");
        user.setUpdated(new Date());
        userDao.addUser(user);
        List<User> users= userDao.findAllUsers();
        String actual=users.get(0).getFirstName();
        Assert.assertEquals("Toshi", actual);
    }

}

My servlet-context.xml has bean initialized. 我的servlet-context.xml已初始化bean。 Servlet-context.xml Servlet-context.xml

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <context:component-scan base-package="net.codejava.spring" />
    <context:component-scan base-package="net.codejava.spring.dao" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url"
            value="jdbc:sqlserver://url;" />
        <property name="username" value="admin_hs" />
        <property name="password" value="" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        <!-- <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop> 
            <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> 
            </props> </property> -->
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <context:annotation-config />
    <bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
    </bean>

</beans>

Error log : - 错误日志:-

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [net.codejava.spring.dao.UserDAOImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[net.codejava.spring.dao.UserDAOImpl]的合格Bean:需要至少1个符合此依赖项的自动装配候选的bean。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 依赖项注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

Try with 试试看

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:servlet-context.xml")
public class UserDAOImplTests { ....

To create a context for your test cases, load only those beans which are required. 要为您的测试用例创建上下文,请仅加载那些必需的bean。 Eg you don't need components like InternalResourceViewResolver, resource mapping for unit testing. 例如,您不需要诸如InternalResourceViewResolver之类的组件,用于单元测试的资源映射。

To achieve this, instead of loading servlet-context.xml completely for your test cases, divide servlet-context.xml into two-three smaller pieces. 为此,不要将servlet-context.xml完全装入您的测试用例中,而是将servlet-context.xml分成两三个较小的部分。

And those xml files only which are required for your test cases. 以及仅用于您的测试用例的那些xml文件。 (As an extra work, you will need to change how you are loading the xml files for dev testing, but that's worth it.) (作为一项额外的工作,您将需要更改加载XML文件以进行开发测试的方式,但这是值得的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM