2013-02-26 103 views
7

मैं अपने कोड का परीक्षण करना चाहता हूं जो AdWords API से कनेक्ट होता है, Google को वास्तविक कॉल किए बिना (वह पैसा खर्च करता है))। कोई विचार है कि मैं TrafficEstimatorServiceInterface के नए कार्यान्वयन में कैसे प्लग कर सकता हूं?नकली adwords api

AdWords क्लाइंट एपीआई निर्भरता इंजेक्शन के लिए गुइस का उपयोग कर रहा है, लेकिन मुझे यकीन नहीं है कि मैं इसे संशोधित करने के लिए इंजेक्टर की पकड़ कैसे प्राप्त कर सकता हूं ?!

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

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

के बजाय जिस तरह से guice इंजेक्शन है बदलने के लिए, तुम सिर्फ TrafficEstimatorServiceInterface का अपना स्वयं का कार्यान्वयन में गुजर और रिकॉर्डिंग करके अपने तरीकों का परीक्षण कर सकते हैं क्या कार्यों पर चलाए जा रहे हैं यह? –

उत्तर

0

आप इस उद्देश्य के लिए एक test account उपयोग करना चाहिए:

यदि यह मदद करता है, यह कैसे मैं अब यह के कार्यान्वयन मिलता है। इसके अलावा, मार्च 2013 के पहले से charge for using the AdWords API नहीं होगा, लेकिन आपको अभी भी अपने टूल को विकसित करते समय एक परीक्षण खाते का उपयोग करना जारी रखना चाहिए।

+2

चूंकि यह उपयोगी जानकारी है, सवाल विशेष रूप से पूछता है कि गुइस निर्भरता इंजेक्शन को कैसे नियंत्रित किया जाए ताकि Google * संपर्क नहीं किया जा सके। मैं एकीकरण परीक्षण के विरोध में इकाई परीक्षण के लिए मानता हूं। –

0

आपको अपने परीक्षण कोड में Google API ऑब्जेक्ट्स का परीक्षण कार्यान्वयन (नकली/स्टब) इंजेक्ट करने की आवश्यकता है। गुइस इंजेक्शन जो Google आंतरिक रूप से उपयोग करता है वह यहां प्रासंगिक नहीं है।

आपको अपना कोड TrafficEstimatorServiceInterface पर निर्भर करना चाहिए और AdWordsServices फैक्टरी से TrafficEstimatorServiceInterface प्राप्त करने के बजाए इसे रनटाइम पर इंजेक्ट करना चाहिए। फिर, अपने यूनिट परीक्षणों में, आप एक नकली या स्टब इंजेक्ट कर सकते हैं।

उदाहरण के लिए मार्टिन फाउलर द्वारा "Inversion of Control Containers and the Dependency Injection pattern" देखें।

यह आपके लिए अभ्यास में कैसे लगेगा, इस पर निर्भर करता है कि आप अपने एप्लिकेशन को चलाने के लिए किस आईओसी कंटेनर का उपयोग कर रहे हैं। आप वसंत बूट उपयोग कर रहे थे, तो यह कुछ इस तरह दिख सकता है:

// 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); 
     } 
    } 
}