2014-02-10 29 views
1

當我嘗試構建項目時遇到了測試階段中的異常。Spring&Maven&JUnit - BeanCreationException:無法自動裝入字段NoSuchBeanDefinitionException:沒有符合條件的類型

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'com.dozortsev.adviceexchange.service.test.UserServiceTest': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: public com.dozortsev.adviceexchange.service.UserService 
com.dozortsev.adviceexchange.service.test.TestContext.userService; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.dozortsev.adviceexchange.service.UserService] 
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)} 

項目由多個模塊:

AdviceExchange 
    ├── domain 
    ├── dao 
    ├── service 
    ├── web 

我不明白,爲什麼發生這種情況,在服務層映射爲@Repository映射爲@Service所有類所有的DAO類。

它看起來像我的應用程序上下文的DAO層:

<beans xmlns="http://www.springframework.org/schema/beans"....> 

    <import resource="classpath:/spring/queries.xml"/> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 

     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/adviceexchange"/> 
     <property name="username" value="root"/> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 

     <property name="dataSource" ref="dataSource"/> 

     <property name="packagesToScan" value="com.dozortsev.adviceexchange.domain"/> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 
       <prop key="hibernate.use_sql_comments">true</prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 

     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
</beans> 

應用程序上下文的測試

<beans xmlns="http://www.springframework.org/schema/beans"...> 

    <import resource="classpath:/spring/dao-application-context.xml"/> 

    <context:annotation-config/> 

    <context:component-scan base-package="com.dozortsev.adviceexchange"/> 

    <!-- 
     Define configuration for embedded database 
     Build HSQL database for unit testing in-memory 
    --> 

    <jdbc:embedded-database id="embeddedDatabase" type="HSQL"> 

     <jdbc:script location="classpath:database/test-ddl.sql"/> 
     <jdbc:script location="classpath:database/test-dml.sql"/> 
    </jdbc:embedded-database> 
</beans> 

,也是我測試類

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath*:/spring/test-application-context.xml") 
public abstract class TestContext { 

    static final Logger log = Logger.getLogger(TestContext.class); 

    @Autowired public UserService userService; 

    private static EmbeddedDatabase db; 

    @BeforeClass public static void setUp() { 
     log.info("creates an HSQL in-memory database"); 
     db = new EmbeddedDatabaseBuilder().setName("embeddedDatabase").build(); 
    } 

    @AfterClass public static void tearDown() { 
     log.info("shutdown database"); 
     db.shutdown(); 
    } 
} 

UserServiceTest是建設項目

public class UserServiceTest extends TestContext { 

    @Test public void testFindByLogin() { 

     final String login = "[email protected]"; 

     Assert.assertNotNull(userService); 
     User user = userService.findByLogin(login); 

     Assert.assertNotNull(user); 
    } 

    @Test public void testCreate() { 

     // prepare data for service 
     User user = new User(
       "Mario", 25, null, "Germany, Dortmund", null, "[email protected]", "gotze_mario", 1 
     ); 
     Assert.assertNull(user.getId()); 
     userService.create(user); 
     Assert.assertNotNull(user.getId()); 
    } 
} 

請幫我解決這個問題的過程中拋出的異常的類。我很感激所有的建議!

+0

什麼是例外發布全部細節 – Dileep

+0

你的問題是什麼? – Rembo

回答

1

此文件夾是錯誤的

https://github.com/dozortsev/AdviceExchange/tree/master/service/src/test/resource 

maven的約定是「資源」 - 以s結尾,所以測試應用程序上下文是不是在類路徑中。

因爲你有類路徑*:/ spring/test-app-context ... with *丟失的文件被忽略,然後它找不到任何bean,因爲上下文是空的。

只需修復文件夾名稱即可。

+0

謝謝!但現在我有另一個例外:'java.lang.IllegalStateException:無法加載ApplicationContext'。 –

+1

如果這是我得到的(@ManyToOne屬性不允許@列):com.dozortsev.adviceexchange.domain.Answer.user)那麼這是一個單獨的問題,你應該開始新的問題。 –

+0

Btw。在github上有源代碼真的有幫助,你應該在你的下一個問題中更多地強調它。 –

相關問題