如何在Play框架单元测试中加载不同的插件? [英] How to load different plugins in Play framework unit tests?
问题描述
我有不同的插件实现了插件
界面。现在我把它们硬编码在play.plugins中这样:
I have different plugins implemented the Plugin
interface. Now I have them hard-coded in play.plugins like this:
100:test.A
200:test.B
但在我的单元测试中,我不希望一次加载它们。换句话说,在测试A中我只想加载插件A而在测试B中只加载B.而不是手动更改配置文件,有没有办法以编程方式更改它?我想当调用 fakeApplication()
时,默认情况下会加载所有插件。
However in my unit tests I don't want both of them to be loaded at one time. In other words, in test A I want to load only plugin A and in test B load B only. Instead of changing the configuration file manually, is there a way to change it programmatically? I guess when fakeApplication()
is invoked all plugins are loaded by default.
推荐答案
您可以使用添加或排除的插件启动 FakeApplication
使用自定义配置。
You can start a FakeApplication
with added or excluded plugins and also with custom configuration.
这是scala中的一个例子(我相信Java API有一个等效的机制):
Here is an example in scala (I believe the Java API has an equivalent mechanism) :
val app = FakeApplication(
withoutPlugins = List("test.A", "test.B"),
additionalPlugins = List("test.C"),
additionalConfiguration = Map("customConfigKey" -> "customConfigValue")
)
在Java中,您可以使用 play.test.Helpers
类中提供的 fakeApplication
静态方法之一。这是scala示例的等价物:
In Java, you can use one of the fakeApplication
static methods available in the play.test.Helpers
class. Here is the equivalent of the scala example :
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import play.test.FakeApplication;
import static play.test.Helpers.*;
Map<String, Object> additionalConfiguration = new HashMap<String, Object>();
additionalConfiguration.put("customConfigKey", "customConfigValue");
List<String> withoutPlugins = Arrays.asList("test.A", "test.B");
List<String> additionalPlugins = Arrays.asList("test.C");
FakeApplication app = fakeApplication(additionalConfiguration, additionalPlugins, withoutPlugins);
这篇关于如何在Play框架单元测试中加载不同的插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!