2015-02-24 41 views
0

我保證我花了很多時間閱讀spring.io上的文檔,嘗試快速入門教程等,但我無法弄清楚我做錯了什麼。我也在這裏看過類似的問題,並嘗試提出的解決方案(在相關的地方)。我敢肯定這簡直太荒唐了,所以我很抱歉不得不問。Spring JPA未能在單元測試之外自動注入/注入bean,我做錯了什麼?

我的資料庫:

@Repository 
public interface SomethingRepository extends CrudRepository<Something, Integer> { 

} 

配置:

@Configuration 
@EnableAutoConfiguration 
@EntityScan("com.where.they.are") //changed for anonymity - assume correct 
@EnableJpaRepositories("com.where.they.are") //changed for anonymity - assume correct 
public class DatabaseConfigurationInTest { 

} 

下面是一些培訓班裏,我試圖注入我的倉庫(失敗),它總是空:

public class SomethingDAO{ 
@Inject 
private SomethingRepository somethingRepository; 
} 

但在這個單元測試中,它工作得很好,注入和一切。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = DatabaseConfigurationInTest.class) 
@ActiveProfiles("local") 
public class SomethingRepoTest{ 
    @Inject 
    private SomethingRepository somethingRepository; 
+1

'SomethingDAO'必須是爲了一個Spring bean爲CDI工作。是嗎? – 2015-02-24 16:21:49

回答

1

要@Predrag馬里奇點,你SomethingDAO類可以用@服務進行批註,並確保它`包是componentScan

可達
@Service 
public class SomethingDAO{ 
@Inject 
private SomethingRepository somethingRepository; 
}