【问题标题】:Mock adwords API模拟adwords API
【发布时间】:2013-02-12 13:33:14
【问题描述】:

我想测试连接到 AdWords API 的代码,而无需实际调用 Google(这需要花钱;))。知道如何插入新的 TrafficEstimatorServiceInterface 实现吗?

AdWords Client API 正在使用 Guice 进行依赖注入,但我不确定如何获取 Injector 以对其进行修改?!

如果有帮助,我现在就是这样实现它的:

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

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

【问题讨论】:

  • 与其改变 guice 的注入方式,您可以通过传入您自己的 TrafficEstimatorServiceInterface 实现并记录在其上运行的操作来测试您的方法吗?

标签: java guice google-ads-api


【解决方案1】:

您应该为此使用test account。此外,从 2013 年 3 月 1 日起,charge for using the AdWords API 将不再存在,但您仍应在开发工具时继续使用测试帐户。

【讨论】:

  • 尽管这是有用的信息,但该问题专门询问如何控制 Guice 依赖注入,以便 联系谷歌。我认为是单元测试而不是集成测试。
【解决方案2】:

您需要将 Google API 对象的测试实现(模拟/存根)注入您的测试代码。 Google 内部使用的 Guice 注入与此处无关。

您应该让您的代码依赖于TrafficEstimatorServiceInterface 并在运行时注入它,而不是让您的代码从AdWordsServices 工厂获取TrafficEstimatorServiceInterface。然后,在您的单元测试中,您可以注入一个 mock 或 stub。

例如,参见 Martin Fowler 的“Inversion of Control Containers and the Dependency Injection pattern”。

这在实践中的表现取决于您用于运行应用程序的 IoC 容器。如果您使用的是 Spring Boot,这可能看起来像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多