2013-02-26 3 views
7

실제 Google에 전화하지 않고 애드워즈 API에 연결하는 코드를 테스트하고 싶습니다 (비용이 듭니다.)). TrafficEstimatorServiceInterface의 새 구현을 어떻게 플러그인 할 수 있습니까?모의 애드워즈 API

애드워즈 클라이언트 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 코드를 가져 오는 대신 코드를 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); 
     } 
    } 
} 
관련 문제