Dropwizard 1.0集成测试:点击外部API

时间:2016-04-06 17:05:22

标签: java api unit-testing integration dropwizard

我正在试图找出如何集成外部API并针对它运行每个集成测试。我一直在阅读和看着:

但看起来这些是测试本地端点而不是外部端点的示例。我希望能够使用JUnit测试来测试我的api调用。目前我不得不启动并运行我的应用程序以确保它们正在运行。

这是我正在探索的方向:

private Client client;

@Before
public void setUp() throws Exception {
    client = ClientBuilder.newClient();
}

@After
public void tearDown() throws Exception {
    client.close();
}

@Test
public void testHitApi() throws Exception {
    client.target("https://api.github.com/users/" + getUser() + "/repos");
}

非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

您需要进行api调用以命中端点。

只做:

client.target("https://api.github.com/users/" + getUser() + "/repos") 

返回一个WebTarget。

理想情况下,您应该执行以下操作:

client
.target("https://api.github.com/users/" + getUser() + "/repos")
.request()
.get() ;   //  for a get call  

谷歌准确的发布/发送/删除电话。

答案 1 :(得分:0)

如果您要针对外部api或api的单独运行实例运行集成测试。

  testEnvironment = new Environment("Test environment", Jackson.newObjectMapper(),
            null, new MetricRegistry(), null);

ObjectMapper mapper = Jackson.newObjectMapper(new YAMLFactory());
    IntegrationTestConfiguration integrationTestConfiguration = mapper.readValue(fixture("integration-testing-config.yml"),
            IntegrationTestConfiguration.class);

实例化您的客户

 exampleClient = new exampleClient(testEnvironment,    clientConfiguration);

希望这有帮助。