2013-02-26 16 views
7

Googleに実際に電話することなく、AdWords APIに接続しているコードをテストしたいと思う。どのように私はTrafficEstimatorServiceInterfaceの新しい実装をプラグインすることができますか?mock adwords api

AdWordsクライアントAPIは依存性注入にGuiceを使用していますが、インジェクタを変更するためにインジェクタをどのように保持できるかわかりません。

それが助け場合は、これは私が今の実装を取得する方法である:

AdWordsServices adWordsServices = new AdWordsServices(); 
    AdWordsSession session = AdwordsUtils.getSession(); 

    TrafficEstimatorServiceInterface trafficEstimatorService = 
      adWordsServices.get(session, TrafficEstimatorServiceInterface.class); 
+0

よりもむしろ操作が動作しているか、あなただけのTrafficEstimatorServiceInterfaceの独自の実装を渡すと、記録することで、あなたのメソッドをテストすることができ、Guiceのが注入される方法を変更それ? –

答えて

0

あなたは、この目的のためにtest accountを使用する必要があります。また、2013年3月1日以降のcharge for using the AdWords APIもなくなりますが、引き続きツールの開発中にテストアカウントを使用する必要があります。

+2

これは有用な情報と同じくらい、Guiceの依存関係注入を制御してGoogleに*連絡しない*ようにする方法が質問されています。私は、統合テストとは対照的に単体テストを想定しています。 –

0

Google APIオブジェクトのテスト実装(モック/スタブ)をテストコードに挿入する必要があります。 Googleが内部的に使用するGuiceの注入は、ここでは関係ありません。

あなたのコードはに依存し、AdWordsServices工場からTrafficEstimatorServiceInterfaceをコードフェッチするのではなく、実行時に注入する必要があります。次に、単体テストでモックまたはスタブを挿入することができます。

Martin Fowlerの "Inversion of Control Containers and the Dependency Injection pattern"を参照してください。

実際にどのように見えるかは、アプリケーションを実行するために使用しているIoCコンテナによって異なります。あなたは春のブートを使用していた場合、これは次のようになります。

// in src/main/java/MyService.java 
// Your service code, i.e. the System Under Test in this discussion 
@Service 
class MyService { 
    private final TrafficEstimatorServiceInterface googleService; 

    @Autowired 
    public MyService (TrafficEstimatorServiceInterface googleService) { 
    this.googleService = googleService; 
    } 

    // The business logic code: 
    public int calculateStuff() { 
    googleService.doSomething(); 
    } 
} 

// in src/main/java/config/GoogleAdsProviders.java 
// Your configuration code which provides the real Google API to your app 
@Configuration 
class GoogleAdsProviders { 
    @Bean 
    public TrafficEstimatorServiceInterface getTrafficEstimatorServiceInterface() { 
    AdWordsServices adWordsServices = new AdWordsServices(); 
    AdWordsSession session = AdwordsUtils.getSession(); 

    return adWordsServices.get(session, TrafficEstimatorServiceInterface.class); 
    } 
} 

// in src/test/java/MyServiceTest.java 
// A test of MyService which uses a mock TrafficEstimatorServiceInterface 
// This avoids calling the Google APIs at test time 
@RunWith(SpringRunner.class) 
@SpringBootTest 
class MyServiceTest { 

    @Autowired 
    TrafficEstimatorServiceInterface mockGoogleService; 

    @Autowired 
    MyService myService; 

    @Test 
    public void testCalculateStuff() { 
     Mockito.when(mockGoogleService.doSomething()).thenReturn(42); 

     assertThat(myService.calculateStuff()).isEqualTo(42); 
    } 

    @TestConfiguration 
    public static class TestConfig { 
     @Bean() 
     public TrafficEstimatorServiceInterface getMockGoogleService() { 
      return Mockito.mock(TrafficEstimatorServiceInterface.class); 
     } 
    } 
} 
関連する問題