Java 使用注入播放 class 的 2.4 编写测试用例
Java Play 2.4 write test case for class using injection
最近我正在为 Java 项目使用 play 2.4 框架。
因为我正在使用 WsClient 库。该库已注入我的 class.
@Inject WSClient wsClient
现在我正在尝试为此编写一个测试用例 class 但由于 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());
}
});
}
在写你有的选项之前,我建议使用elastic4s。
这个第三方库将帮助您编写更实用的代码,并为您提供非常好的 dsl 来编写您的查询。
还有一件事,我不知道您使用 elasticsearch 的用例是什么,但我建议您使用与其他客户端不同的客户端 api,这将为您提供更安全的连接和更高效的连接。
你得到了 NPE,因为你自己用 new 实例化了 ElasticSearch,并且没有让 guice 为你布线,这就是 WSClient 为空的原因。
现在供您选择,
您有 2 个选项:
将 WithApplication 添加到您的测试中,这将基本上加载您的应用程序,这将使您能够访问 Guice 注入器,您可以从中使用 ElasticSearch class,您有两种方法可以做到这一点:
如剧中所述documentation使用
import play.api.inject.guice.GuiceInjectorBuilder
import play.api.inject.bind
val injector = new GuiceInjectorBuilder()
.configure("key" -> "value")
.bindings(new ComponentModule)
.overrides(bind[Component].to[MockComponent])
.injector
val elasticsearch = injector.instanceOf[ElasticSearch]
通过导入播放
import play.api.Play
val elasticsearch = Play.current.injector.instanceOf(classOf[ElasticSearch])
使用 FakeApplication:只需掌握假应用程序注入器,并使用它来获取 ElasticSearch class.
我不喜欢上面的选项,因为你需要一个应用程序 运行ning,这会使你的测试非常慢。
我建议您自己创建 WSClient 并用它实例化 ElasticSearch class,然后 运行 您的测试。
val httpClient = NingWSClient.apply()
val elasticsearch = new ElasticSearch(httpClient)
这是一个更轻便的解决方案,应该会使您的测试 运行 更快。
最近我正在为 Java 项目使用 play 2.4 框架。
因为我正在使用 WsClient 库。该库已注入我的 class.
@Inject WSClient wsClient
现在我正在尝试为此编写一个测试用例 class 但由于 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());
}
});
}
在写你有的选项之前,我建议使用elastic4s。 这个第三方库将帮助您编写更实用的代码,并为您提供非常好的 dsl 来编写您的查询。 还有一件事,我不知道您使用 elasticsearch 的用例是什么,但我建议您使用与其他客户端不同的客户端 api,这将为您提供更安全的连接和更高效的连接。
你得到了 NPE,因为你自己用 new 实例化了 ElasticSearch,并且没有让 guice 为你布线,这就是 WSClient 为空的原因。
现在供您选择, 您有 2 个选项:
将 WithApplication 添加到您的测试中,这将基本上加载您的应用程序,这将使您能够访问 Guice 注入器,您可以从中使用 ElasticSearch class,您有两种方法可以做到这一点:
如剧中所述documentation使用
import play.api.inject.guice.GuiceInjectorBuilder import play.api.inject.bind val injector = new GuiceInjectorBuilder() .configure("key" -> "value") .bindings(new ComponentModule) .overrides(bind[Component].to[MockComponent]) .injector val elasticsearch = injector.instanceOf[ElasticSearch]
通过导入播放
import play.api.Play val elasticsearch = Play.current.injector.instanceOf(classOf[ElasticSearch])
使用 FakeApplication:只需掌握假应用程序注入器,并使用它来获取 ElasticSearch class.
我不喜欢上面的选项,因为你需要一个应用程序 运行ning,这会使你的测试非常慢。 我建议您自己创建 WSClient 并用它实例化 ElasticSearch class,然后 运行 您的测试。
val httpClient = NingWSClient.apply()
val elasticsearch = new ElasticSearch(httpClient)
这是一个更轻便的解决方案,应该会使您的测试 运行 更快。