2016-05-11 4 views
4

ユニットテストケースで使用するスプリングマネージドBeanを自動配線しようとしています。しかし、autowired beanは常にnullです。私の設定は以下の通りです。ユニットテストケースの@Autowired beans

私のユニットテストクラス

@RunWith(MockitoJUnitRunner.class) 
@ContextConfiguration(locations = "classpath*:business-context-test.xml") 
public class SMSGatewayTest { 

    @Autowired 
    @Qualifier("mySMSImpl") 
    private ISMSGateway smsGateway; 

     @Test 
     public void testSendTextMessage() throws Exception { 
      smsGateway.sendText(new TextMessage("TEST")); 
      // ^___________ this is null, even though I have set ContextConfiguration 
     } 

} 

春は、Bean管理

package com.myproject.business; 

@Component("mySMSImpl") 
public class MySMSImpl implements ISMSGateway { 

    @Override 
    public Boolean sendText(TextMessage textMessage) throws VtsException { 
      //do something 
    } 

} 

ユニット・テスト・ケースのコンテキスト

<?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.2.xsd 

     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 


     <context:annotation-config/> 
     <context:component-scan base-package="com.myproject.business"/> 
</beans> 

私は多くの疑問を抱いており、私はすでに自分のコードに組み込んでいる答えをすべて提供しています。私が何が欠けているか教えてくれる人もいます。私はテストケースを実行するためにintellij 14を使用しています。

答えて

3

次のようなコードを持っている:私は、クラス内の任意のより多くのモックを見ていないですよう

@RunWith(MockitoJUnitRunner.class) 
@ContextConfiguration(locations = "classpath*:business-context-test.xml") 
public class SMSGatewayTest { 
    .. 
    .. 
} 

あなたは本当にMockitoJUnitRunnerを使用しwannnaください。

などでのJUnitを実行してみてください

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath*:business-context-test.xml") 
public class SMSGatewayTest { 
    .. 
    .. 
} 

編集 -

@RunWith(SpringJUnit4ClassRunner.class)はに@ContextConfiguration(..)を使用して宣言されている利用可能なすべてのそれらの依存関係を作りますそれが使用されるクラス。 SMSGateway - 例えば

は、あなたのケースでは、クラスの@RunWith(SpringJUnit4ClassRunner.class)を持っています。 @ContextConfiguration(locations = "classpath *:business-context-test.xml")を使用して設定されたSMSGatewayへの依存関係をすべて利用できるようにします。

これは、クラスSMSGateway内で 'smsGateway'を自動生成するのに役立ちます。 @ RunWith(MockitoJUnitRunner.class)を使用していたので、この依存関係はオートワイヤリングでは利用できませんでした。したがって、春は不平を言っていました。アプリケーションでMockitoを使用している場合は、MockitoJUnitRunner.classが必要です。クラスを嘲笑していないので、今のところMockitoJUnitRunnerは必要ありません。

詳細については、Mockito, JUnit and Springを参照してください。

http://www.alexecollins.com/tutorial-junit-rule/をご覧ください。これは、@Runwithと@ContextConfigurationの注釈が裏でどのように機能するかを理解するのに役立ちます。

+0

クール。ありがとう、それは働いた。 – ManinGreen

+0

問題が実際にどのようなものだったのか、どうしてなぜSpringJUnit4ClassRunnerへの変更が問題を解決したのかなど、あなたの答えに詳細を追加できたらうれしいですね。私はここで少し混乱している。 :) – ManinGreen

+0

確かに答えを編集しました。見てください。 – asg

0

ビーン名に余分な空白

@Component("mySMSImpl ") 
@Qualifier("mySMSImpl") 

なぜあなたは、この場合にQualifierアノテーションを使用しますがありますか? ISMSGatewayインターフェイスが複数実装されていますか?

+0

はい複数の実装があります。既にそのスペースを削除しました。 – ManinGreen

+0

うん、それはタイプミスだった。私の悪い。 – ManinGreen

+0

@ManinGreen ''をコンテキスト設定に追加してみてください。 Springが私のテスト固有のコンテキストファイル内のクラス –

関連する問題