我写了一个spring mvc web应用程序。应用程序运行正常。 我正在尝试使用spring Test套件编写DAO测试。我使用的是与web-app相同的配置文件servlet-context.xml。当我运行测试时,我得到没有类型错误的合格bean
我查看了stackoverflow,但它“显示”我的配置是正确的。请建议: - 找到下面的代码: -
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);
}
}
我的servlet-context.xml已初始化bean。 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>
错误日志: -
引起: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 找到[net.codejava.spring.dao.UserDAOImpl]类型的限定bean 对于依赖:预计至少有1个bean有资格作为autowire 这种依赖的候选人。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(所需=真)}
答案 0 :(得分:0)
尝试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:servlet-context.xml")
public class UserDAOImplTests { ....
答案 1 :(得分:0)
要为测试用例创建上下文,请仅加载所需的bean。例如。你不需要像InternalResourceViewResolver这样的组件,用于单元测试的资源映射。
要实现这一点,不要完全为您的测试用例加载servlet-context.xml,而是将servlet-context.xml分成两到三个较小的部分。
只有那些测试用例所需的xml文件。 (作为额外的工作,您需要更改加载xml文件以进行开发测试的方式,但这是值得的。)