2016-10-10 8 views
0

Jpaリポジトリ(CrudRepositoryインターフェイス)を使用するSpring Appがあります。新しいSpringテスト構文@WebMvcTest(MyController.class)を使用してコントローラをテストしようとすると、Jpaリポジトリを使用するサービスクラスのインスタンスを作成しようとすると失敗します。私はそれを実行すると、アプリが動作します。ここでSpring Annotation @WebMvcTestはJpaリポジトリを持つアプリケーションでは機能しません

は誤りです:

*************************** 
APPLICATION FAILED TO START 
*************************** 

Description: 

Parameter 0 of constructor in com.myapp.service.UserServiceImpl required a bean of type 'com.myapp.repository.UserRepository' that could not be found. 

Action: 

Consider defining a bean of type 'com.myapp.repository.UserRepository' in your configuration. 
+0

pom.xmlを共有し、スタックトレースを深めて深く掘り下げてください。 –

答えて

1

Spring MVCのコンポーネントのみを適用します。このannotionドキュメント

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MVC tests (i.e. @Controller, @ControllerAdvice, @JsonComponent Filter, WebMvcConfigurer and HandlerMethodArgumentResolver beans but not @Component, @Service or @Repository beans).

によります。

アプリケーション構成全体をロードしてMockMVCを使用する場合は、@SpringBootTestとこの注釈ではなく@AutoConfigureMockMvcを組み合わせて使用​​する必要があります。

+0

はい、あなたが正しいです、私はあなたが言及した方法を使用して終了し、それは動作します。 – dickyj