2017-09-05 267 views
0

私は別のユーティリティクラスに依存するDAOクラスを持っていますAuditStore原因:org.springframework.beans.factory.UnsatisfiedDependencyException: 'myAppDao'という名前のBeanを作成中にエラーが発生しました:

package myapp; 
@Repository 
public class MyAppHibernateDao { 

    @Autowired 
    public void setAuditStore(AuditStore auditStore) { 
     ConnectorLoggingHelper.setAuditStore(auditStore); 
    } 

} 

AuditStore.java

package myapp; 
@Resource 
public class AuditStore { 
    //too many dependencies in this class including db connection 
} 

今、私は 'AuditStore' の機能をカバーしていないDAOクラスの統合テストを書きたいです。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:META-INF/spring-myapp-db-connector-test.xml") 
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class}) 
public class MyAppHibernateDaoIntegrationTest { 


    @Test 
    public void test() { 
     //test code here 
    } 
} 

と私のXML設定ファイルには、私は次のエラーを取得しています、これを実行するWHE

<!--spring-myapp-db-connector-test.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" xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 
    <!-- enable autowiring --> 

<context:annotation-config/> 

<bean id="myAppDao" class="myapp.MyAppHibernateDao"> 

</beans> 

です。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myAppDao': Unsatisfied dependency expressed through method 'setAuditStore' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'myapp.AuditStore' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 

AuditStoreは、非常に複雑なオブジェクトであると私は(私はnullまたはこのクラスのモックを必要とする)テストのために、このクラスの機能を使用しないでください。 AuditStoreのbeanをxmlで定義して作成しないようにする方法はありますか?

私は、を作ってみるとテスト用のアプリケーションコードが変更されることが分かりましたので、他のオプションを探しています。

代替手段がある場合はお手伝いください。

+0

あなたはすでにあなた自身の質問に答えています...モックを使用してください。 –

+0

xmlでAuditStoreクラスのこのモックオブジェクトを定義する方法は? – Edayan

答えて

1

可能であれば、アノテーション駆動型設定に移行し、@InjectMockのようなものを使用することを検討する必要があります。しかし、あなたはあなたがあなたの質問に概説されたアプローチに固執する必要があると仮定すると、あなたはいくつかの方法でspring-myapp-db-connector-test.xmlMyAppHibernateDaoのモックインスタンスを定義することができます。

  • Use a factory bean
  • 使用SpringockitoMyAppHibernateDaoIntegrationTest

    <bean id="myAppDao" class="org.mockito.Mockito" factory-method="mock"> 
        <constructor-arg value="myapp.MyAppHibernateDao"/> 
    </bean> 
    

あなたがすることができ、その後@Autowire MyAppHibernateDaoをし、テスト/セットアップ方法でそれに期待などを設定しますと、以下のように当然のことながら、

  • spring-myapp-db-connector-test.xmlmyAppDao Beanを宣言します。

  • +0

    アイデアはMyAppHibernateDaoではなくAuditStoreをモックすることですが、このアプローチはうまくいきます。ありがとう、ありがとうございます。 – Edayan

    関連する問題

     関連する問題