2016-09-10 79 views
5

@MockBeanアノテーションの使用に問題があります。ドキュメントでは、MockBeanがコンテキスト内のBeanを置き換えることができますが、私のユニットテストではNoUniqueBeanDefinitionExceptionが発生しています。注釈の使用方法はわかりません。私がレポを模擬することができれば、明らかに複数のBean定義が存在します。私は例を、以下のいSpringブートテストでMockBeanアノテーションが発生すると、NoUniqueBeanDefinitionExceptionが発生します。

はこちらをご覧ください:

public interface MyMongoRepository extends MongoRepository<MyDTO, String> 
{ 
    MyDTO findById(String id); 
} 

ジャージーリソース:

@Component 
@Path("/createMatch") 
public class Create 
{ 
    @Context 
    UriInfo uriInfo; 

    @Autowired 
    private MyMongoRepository repository; 

    @POST 
    @Produces(MediaType.APPLICATION_JSON) 
    public Response createMatch(@Context HttpServletResponse response) 
    { 
     MyDTO match = new MyDTO(); 
     match = repository.save(match); 
     URI matchUri = uriInfo.getBaseUriBuilder().path(String.format("/%s/details", match.getId())).build(); 

     return Response.created(matchUri) 
       .entity(new MyResponseEntity(Response.Status.CREATED, match, "Match created: " + matchUri)) 
       .build(); 
    } 
} 

とJUnitテスト:

https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

私はMongoのリポジトリを持っています

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class TestMocks { 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @MockBean 
    private MyMongoRepository mockRepo; 

    @Before 
    public void setup() 
    { 
     MockitoAnnotations.initMocks(this); 

     given(this.mockRepo.findById("1234")).willReturn(
       new MyDTO()); 
    } 

    @Test 
    public void test() 
    { 
     this.restTemplate.getForEntity("/1234/details", MyResponseEntity.class); 

    } 

} 

エラーメッセージ:

Field repository in path.to.my.resources.Create required a single bean, but 2 were found: 
    - myMongoRepository: defined in null 
    - path.to.my.MyMongoRepository#0: defined by method 'createMock' in null 

答えて

8

それはバグです:https://github.com/spring-projects/spring-boot/issues/6541

あなたは、その名前でモックを宣言することによって、それを回避することができます。あなたのコメントに応えて

@MockBean(name="myMongoRepository") 
private MyMongoRepository repository; 

Fr OM Spring's doc:便宜上

、RESTが開始 サーバー 実行しているサーバーへの相対リンクを解決しますすることができ、更に@Autowire TestRestTemplateに呼び出しを行う必要がありますテスト。これを読んで

、私はあなたのウェブ環境で@SpringBootTestを宣言する必要があると思う:あなたの春のブートは、その後TestRestTemplateの必要性は何である、ウェブ環境を起動しない場合

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 

。したがって、私は春はそれを利用できるようにしないと思います。

+0

ありがとうございました。私はそれを試みましたが、今度はSpringがTestRestTemplateをautowireできません:org.springframework.beans.factory.UnsatisfiedDependencyException: 'path.to.my.tests.TestMocks'という名前のBeanを作成中にエラーが発生しました:フィールド 'restTemplate'で表現されている不満足な依存性:依存関係[org.springframework.boot.test.web.client.TestRestTemplate]の型のbeanが見つかりました[org.springframework.boot.test.web.client.TestRestTemplate]:この依存関係のautowire候補となる少なくとも1つのbeanが必要です。 – JCN

+0

がTestResttemplateの答えを更新しました – alexbt

+0

ああ、私はウェブ環境のものを削除して忘れてしまった。私はそれを再追加しました、そして、今私のDTOクラスのための適切なHttpMessageConverterについてのエラーを取得しています。だから私は元のエラーを過ぎて、HttpMessageConvertersについて学んでいるように見えます。ありがとう! – JCN

関連する問題