2015-11-05 49 views
0

編輯2:我已經設法「解決」了這個問題......只是刪除Autowired註釋並使用setUp/tearDown配置。所以,爲了補充我的問題,爲什麼沒有自動裝配的註釋工作(類找到路徑)並且它沒有?我在測試環境文件中做錯了什麼?jUnit在spring服務類中:沒有這樣的bean定義

編輯:我要指出,我試圖autowire是一個類實現兩個接口。我看過(論壇帖子),你不能這樣做(所以這可能是我的問題的原因),但沒有設法得到一個實際的,「官方」的答案。 因此,對於我所看到的,我應該「自動裝載」一個參考界面和另一個參考界面,而Spring會奇蹟般地發現我試圖測試這個特定的實現。是對的嗎?如果是這樣,爲什麼?

我正在研究一個有點大的J2EE項目的jUnit測試,在Hibernate上沒有任何經驗。我打算將每個軟件包打包,使用大多數是預配置的文件來進行測試,所以配置的確切細節對我來說有點模糊。但我無法實例化一個定義的類(它被標記爲服務)。

我有這樣的 「被測試」 類:

package com.stackoverflow.business.service.impl; 

@service 
public class ClassA Implements InterfaceA<BeanA>, BeanB { 
/* Actual implementation of the class*/ 
} 

,然後這個,實際測試類,請不要介意無用進口:

package com.stackoverflow.business.service.test; 

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

import com.stackoverflow.business.service.impl.ClassA; 


@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { 
     "classpath:/spring/integration-core-config.xml", 
     "classpath:/spring/app-custom-persistence-hibernate.xml", 
     "classpath:/spring/app-test-persistence-hibernate.xml", 
     "classpath:/spring/app-custom-reporting.xml", 
     "classpath:/spring/test-context.xml"}) 
public class ClassATest{ 
@Autowired 
    private ClassA classATestInstance; 
/* Rest of the implementations*/ 
} 

AFAIK,4個第一個xml文件是測試的標準。測試的context.xml被我加入,在希望春天發現了什麼,我想測試,與下面的配置(它似乎沒有同樣的錯誤這個XML結果運行測試):

<?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:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> 

    <context:component-scan base-package="com.stackoverflow.business.service.impl" /> 

    <bean id="ClassATest" class="com.stackoverflow.business.service.impl"></bean> 

</beans> 

希望那裏沒有錯別字。方向是正確的,包名是正確的,但是當spring嘗試創建ClassA實例(ClassATestInstance)時,我得到一個BeanCreationException,告訴我它沒有找到ClasA bean。我從來沒有試過用@Service註釋來測試一個類,所以我不確定即使我可以/應該這樣做,但是對於同一個包中的以前的類(儘管沒有註釋),使用test-context.xml文件來定義這個bean工作得很好。

我在這裏錯過了什麼?

編輯:添加異常文本(僅第一線以避免混亂):

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'com.stackoverflow.business.service.test.ClassATest' 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private com.stackoverflow.business.service.impl.ClassA com.stackoverflow.business.service.test.ClassATest nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.stackoverflow.business.service.impl.ClassA] 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)} 
+0

異常,請。 –

+0

這很清楚:沒有匹配的bean。如果它是可行的,嘗試使用構造函數而不是字段注入,然後在普通模擬的本地構造服務對象。 – chrylis

+0

我知道那裏沒有匹配的bean,我想知道的是爲什麼系統告訴我沒有匹配的bean(如果有的話)(路徑和內容是正確的)。做ClassA實例= new ClassA();創建沒有問題的實例。這種情況下有什麼不兼容的「自動裝配」? – Neuromante

回答

0

你可以嘗試標註ClassATest@WebAppConfiguration如下:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { 
      "classpath:/spring/integration-core-config.xml", 
      "classpath:/spring/app-custom-persistence-hibernate.xml", 
      "classpath:/spring/app-test-persistence-hibernate.xml", 
      "classpath:/spring/app-custom-reporting.xml", 
      "classpath:/spring/test-context.xml"}) 
@WebAppConfiguration 
public class ClassATest{ 
+0

不幸的是,我們使用的是Spring 3.1.3版本,似乎註釋已經包含在[3.2](http://docs.spring.io/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/ api/org/springframework/test/context/web/WebAppConfiguration.html)版本。 – Neuromante

相關問題