我刚刚开始使用Guice和Play,所以我想这是一个很长但基本的问题。我在这里检查了指南:http://eng.42go.com/play-framework-dependency-injection-guice/但我不知道为什么我的代码会失败。
首先,我有一个全局注射器:
public class GlobalInjector {
private static Injector guiceInjector;
private static List<AbstractModule> modules = new ArrayList<AbstractModule>();
public static Injector getInjector() {
return guiceInjector;
}
public static loadModules() {
guiceInjector = Guice.createInjector(modules);
}
public static addModule(AbstractModule module) {
modules.add(module);
}
}
此外,我通过扩展GlobalSettings
类(也修改了application.global)将Guice添加到Play中
public class GuiceExtendedSettings extends GlobalSettings {
@Override
public void onStart(Application app) {
GlobalInjector.loadModules();
}
@Override
public <A> A getControllerInstance(Class<A> controllerClass) {
return GlobalInjector.getInjector().getInstance(controllerClass);
}
}
然后我将我的测试模块作为Play中的插件(省略了一些必需的方法,因为它们在这篇文章中什么都不做):
public class TestModule extends AbstractModule implements Plugin {
@Override
public void configure() {
// Worker is a simple class
Worker worker = new SimpleWorker();
MapBinder<String, Worker> mapBinder = MapBinder.newMapBinder(binder(), String.class, Worker.class);
mapBinder.addBinding(worker.getName()).toInstance(worker);
}
@Override
public void onStart() {
GlobalInjector.addModule(this);
}
}
Worker
是一个简单的界面:
public interface Worker {
public String getName();
public String getResult();
}
SimpleWorker
:
public class SimpleWorker implements Worker {
public String getName() {
return "SimpleWorker";
}
public String getResult() {
return "works";
}
}
这是显示控制器逻辑的代码片段:只需在注入的地图中打印所有工作者结果
public class TestController extends Controller {
@Inject
Map<String, Worker> workers;
public Result showWorkers() {
StringBuilder sb = new StringBuilder();
for (Worker worker : workers) {
sb.append(worker.getName() + ": " + worker.getResult() + "</br>");
}
return ok(sb.toString()).as("text/html");
}
}
行。为了使这项工作,我将以下行放在play.plugins:
100:test.TestModule
我的想法是: Play加载插件(TestModule) - &gt; TestModule将自己添加到GlobalInjector - &gt; GlobalInjector创建Guice注入器 - &gt; Guice将地图注入控制器
然而结果是
Guice没有注入地图。地图仍为null
。
我该如何测试?(即如何将不同的工作人员注入该地图?我在上面的代码中对该部分进行了硬编码。但我正在寻找一种动态的方法使用不同的模块。)
public class Test {
@Test
public void testInjector() {
running(fakeApplication(), new Runnable() {
public void run() {
// how can I inject using different modules here?
}
});
}
}
答案 0 :(得分:1)
您需要使用fakeApplication helper方法,该方法允许您指定全局设置对象和其他插件。有关详细信息,请参阅http://www.playframework.com/documentation/api/2.1.x/java/play/test/Helpers.html#fakeApplication(java.util.Map,%20java.util.List,%20play.GlobalSettings)。
但基本上,你的测试看起来应该是这样的:
public class Test {
@Test
public void testInjector() {
Map<String, Object> config = new HashMap<String, Object>();
// add any additional config options, e.g. in-memory db
List<String> plugins = new ArrayList<String>();
plugins.add("full.package.name.TestModule");
GlobalSettings global = null;
try {
global = (GlobalSettings) Class.forName("full.package.name.GuiceExtendedSettings").newInstance();
} catch(Exception e) {}
running(fakeApplication(config, plugins, global), new Runnable() {
public void run() {
// do some assertions
}
});
}
}
您还需要确保guice实例化测试控制器,否则将不会注入worker map。