2017-09-18 6 views
0

私はspockフレームワークで仕様クラスを作成しています。Spring @Autowiredオブジェクトがnullです

@ContextConfiguration(classes = [MyServiceImplementation.class]) 
    class MyServiceSpecification extends Specification { 

     @Autowired 
     private MyService myServiceImplementation 

     def " " { 
      //code 
     } 
    } 

クラスMyServiceImplementation@Service注釈されています。私はXML設定を使用していません。 MyServiceImplは、インターフェイスの実装です:MyService

なぜautowiredオブジェクトmyServiceImplementationはnullですか?

ComponentScanを使ってみましたが、それでも動作しませんでした。

+0

あなたのコードのどこかに「新しい」MyServiceSpecificationを行っているような音がします(またはSpockはこれをやっています)! – MystyxMac

+0

回避策はありますか?このフィールドのヌル値にどのように影響するかわかりません – Luca

+0

Spock Spring Module(http://spockframework.org/spock/docs/1.1/all_in_one.html#_spring_module)を使用していますか? – MystyxMac

答えて

0

まず、クラスパスにspock-corespock-springの両方を設定する必要があります。次に、@ContextConfiguration(classes=は、Beanクラスではなく構成クラスのリストを取ります。

@ContextConfiguration(classes = [MyConfig.class]) 
class MyServiceSpecification extends Specification { 

    @Autowired 
    private MyService myServiceImplementation 

    def " " { 
     //code 
    } 
} 

// You could also define @ComponentScan here 
class MyConfig { 
    @Bean 
    MyService myService() { 
     return new MyServiceImplementation(); 
    } 
} 
+0

私は変更を適用し、それは、ソリューションのおかげで働いた – Luca

関連する問題