2016-12-31 3 views
0

私はspring mvc Webアプリケーションを作成しました。アプリケーションは正常に実行されます。 私はspringテストスイートを使用してDAOテストを作成しようとしています。 web-appで使用されているのと同じconfigファイルservlet-context.xmlを使用しています。テストを実行すると、タイプの適格なBeanはありません。"適格タイプのBeanはありません" spring mvcアプリケーションのDAOテスト中

私は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("[email protected]"); 
     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); 
    } 

} 

私のサーブレットのcontext.xmlは、Beanが初期化されています。 サーブレットの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]は依存関係のために を見つけました:この依存関係のためにautowire の候補となる少なくとも1つのbeanが必要です。依存関係の注釈: {@ org.springframework.beans.factory.annotation.Autowired(必要=真)}

+1

'classpath:*/servlet-context.xml'は正しいですか?あなたのservlet-context.xmlはどこですか? – alexbt

+0

コンポーネントスキャン - <コンテキスト:コンポーネントスキャンベースパッケージ= "net.codejava.spring.dao" /> - は冗長です。あなたはそれを取り除くことができます。 – asg

+0

'UserDAOImpl'クラスも投稿できますか?また、 'UserDAO'を実装/拡張している他のクラスはありますか? – AntJavaDev

答えて

0

は、それらの負荷、あなたのテストケースのコンテキストを作成するには

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

で試してみてください必要な豆。例えば。ユニットテストのためのリソースマッピングであるInternalResourceViewResolverのようなコンポーネントは必要ありません。

これを実現するには、テストケースのためにservlet-context.xmlを完全にロードするのではなく、servlet-context.xmlを2つの小さな断片に分割します。

テストケースに必要なxmlファイルのみです。 (余分な作業として、開発者テストのためにXMLファイルをどのようにロードするのかを変更する必要がありますが、それだけの価値があります)

関連する問題