【发布时间】:2016-02-10 14:58:51
【问题描述】:
最近我在 Java 项目中使用 play 2.4 框架。
我正在使用 WsClient 库。该库已注入我的班级。
@Inject WSClient wsClient
现在我正在尝试为该类编写一个测试用例,但由于 wsClient 变量的空指针错误,测试用例失败。
wsClient.url("some url").get()
你能帮我解决这个问题吗?
下面是测试代码
// Class
public class ElasticSearch {
@Inject WSClient wsClient;
public Promise<WSResponse> createIndex() {
Logger.info("Entering ElasticSearch.createIndex()");
Logger.debug("WSClient: " + wsClient);
Promise<WSResponse> response =wsClient.url(this.getEsClient()+ "/" +this.getEsIndexName()).setContentType("application/json").put("");
Logger.info("Exiting ElasticSearch.createIndex()");
return response;
}
}
// Test function
@Test
public void testCreateIndex() {
running(fakeApplication(), new Runnable() {
public void run() {
ElasticSearch esearch= new ElasticSearch();
esearch.setEsIndexName("car_model");
assertNotNull(esearch.createIndex());
}
});
}
【问题讨论】:
-
可以分享一下测试代码吗?
-
您好,Tomer,感谢您的回复,我已修改问题并添加了测试代码。
-
你想测试弹性搜索功能吗?意思是你加载一个实际的弹性搜索集群并想要查询它?或者您想“模拟”来自 wsClinet 的重新调整的结果?
-
我想测试我创建的 ElasticSearch 类功能。我正在使用 WSClient 向 ElasticSearch DB 发送调用
标签: java dependency-injection playframework-2.4