2017-07-09 5 views
3

テストクラスにentityManagerを注入する際に問題があります。これは私のテストクラスで私の春のコンテキストを読み込むことができないためだと思う。バネのテストユニットに@PersistenceContextを挿入します

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'noteDeFraisDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available 

が、これは私のテストクラス

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = {NoteDeFraisTestConfig.class}) 
@Transactional 

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) 
public class NoteDeFraisDAOIT 
{ 
    @Autowired 
    private NoteDeFraisDAO noteDeFraisDAO; 

    @Test 
    public void myTest() 
    { 

    } 

} 

であり、これは私が新しい使用する場合entitiesManagerのない注入はDAOで定義されていないが存在することを知っているテストのための設定ですが、私にはありません私は何をすべきか知っている。

@Configuration 
public class NoteDeFraisTestConfig { 

    @Bean 
    NoteDeFraisDAO noteDeFraisDAO() 
    { 
     return new NoteDeFraisDAO(); 
    } 
} 

私は自分のApplicationContextに私のContextConfigurationを設定しようとしたが、それは私がディレクトリWEB-INFがクラスパスに属していないので、それがあると思います動作しませんでした。どうすればこの問題を解決できますか?

これは私のプロジェクト enter image description here

感謝の構造です。

更新

これが私の最後のテストクラスである

@WebAppConfiguration 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"file:web/WEB-INF/applicationContext.xml", "file:web/WEB-INF/db-config.xml","file:web/WEB-INF/dispatcher-servlet.xml","file:web/WEB-INF/security-config.xml"}) 
@Transactional 
public class NoteDeFraisDAOIT 
{ 
    @Autowired 
    private NoteDeFraisDAO noteDeFraisDAO; 

    @Test 
    public void myTest() 
    { 

    } 

} 

答えて

1

うん、問題は、あなたのテストのためにロードさApplicationContextLocalContainerEntityManagerFactoryBeanが含まれていないということです。それがapplicationContext.xmlで正しく宣言されていると仮定すると、下のリンクされた解決策が役立ちます。

私のContextConfigurationを自分のapplicationContextに設定しようとしましたが、動作しませんでした。ディレクトリWEB-INFがクラスパスに属さないためだと思います。どうすればこの問題を解決できますか? Location of spring-context.xml

:ここではカバーされています

関連する問題