【问题标题】:Dropwizard 1.0 Integration Testing: Hitting an external APIDropwizard 1.0 集成测试:使用外部 API
【发布时间】:2016-07-27 05:57:00
【问题描述】:
我试图弄清楚如何集成外部 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");
}
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签:
java
api
unit-testing
integration
dropwizard
【解决方案1】:
您需要进行 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
google 以获得准确的 post/put/delete 调用。
【解决方案2】:
如果您打算针对外部 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);
希望这会有所帮助。