Play Framework 2.7.0 Config,无需通过application.conf进行常规配置即可进行测试

时间:2019-02-08 16:09:20

标签: java testing playframework guice play-framework-2.7

在Play的application.conf中,我有一些针对我的应用程序的配置。例如:

pew.server-script {
  python {
    exec = "python {1}"
    filename = "{1}.py"
  }
}

在Play从2.6.20迁移到2.7.0的过程中,我发现测试不起作用,因为pew.server-script不在Config中。 这是一个示例测试:

private final Application application = prepareApplication();

static Application prepareApplication() {
    Map<String, Object> additional = new HashMap<>(inMemoryDatabase("prototype"));
    // skipped
    additional.put("pew.scriptsFolder", "example");
    // skipped
    return new GuiceApplicationBuilder()
            .overrides(Bindings.bind(HttpExecutionContext.class).to(
                    () -> new HttpExecutionContext(ForkJoinPool.commonPool())
            ))
            .configure(ConfigFactory.parseMap(additional))
            .build();
    }

@Test
public void regAndLogin() {
    running(application, () -> {
        registerUser(application);
        Http.RequestBuilder request = fakeRequest()
                .method(GET)
                .host("localhost")
                .header("Authorization", basicKey)
                .uri("/user/test");
        Result result = route(application, request);
        assertEquals(OK, result.status());
    });
}

调用GuiceApplicationBuilder.build()时,在初始化模块时失败:

bind(ConfigService.class).asEagerSingleton();

ConfigService看起来像这样:

@Singleton
public class ConfigService {
   private Config config;
   // skipped

   @Inject
   public ConfigService(Config config) {
       this.config = config;
       decodeSettings();
   }

   @SuppressWarnings("unchecked")
   private void decodeSettings() {
       Map<String, Map<String, String>> cfg = (Map<String, Map<String, String>>) config.getAnyRef("pew.server-script");
       // skipped
   }
   // skipped
}

例外:

com.google.inject.CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, com.typesafe.config.ConfigException$Missing: No configuration setting  found    for key 'pew.server-script'
  at services.ConfigService.<init>(ConfigService.java:17)
  at Module.configure(Module.java:24) (via modules: com.google.inject.util.Modules$OverrideModule ->  Module)
  while locating services.ConfigService

1 error

    at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:543)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:186)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:109)
    at com.google.inject.Guice.createInjector(Guice.java:87)
    at com.google.inject.Guice.createInjector(Guice.java:78)
    at play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:186)
    at play.inject.guice.GuiceBuilder.injector(GuiceBuilder.java:210)
    at play.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.java:115)
    at controller.ApplicationBuilder.prepareApplication(ApplicationBuilder.java:51)
    at controller.ApplicationBuilder.prepareApplication(ApplicationBuilder.java:27)
    at controller.UnitTests.<init>(UnitTests.java:38)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(    NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(    DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key     'pew.server-script'
    at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:156)
    at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:174)
    at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:180)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:188)
    at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:193)
    at com.typesafe.config.impl.SimpleConfig.getAnyRef(SimpleConfig.java:279)
    at services.ConfigService.decodeSettings(ConfigService.java:24)
    at services.ConfigService.<init>(ConfigService.java:19)
    at services.ConfigService$$FastClassByGuice$$a3848b63.newInstance(<generated>)
    at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(    DefaultConstructionProxyFactory.java:89)
    at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
    at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
    at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:306)
    at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(    ProviderToInternalFactoryAdapter.java:40)
    at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
    at com.google.inject.internal.InternalFactoryToProviderAdapter.get(    InternalFactoryToProviderAdapter.java:39)
    at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(    InternalInjectorCreator.java:211)
    at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:182)
    ... 38 more 

当路由器使用config和JPA内容中的设置创建路由器时,Akka也崩溃了-Config中未包含必要的设置。我已经通过IntelliJ IDEA调试进行了检查-在配置实例中的“ pew”中只有pew.scriptFolder =“ example”,已在其他配置(prepareApplication()函数)中进行了设置。
我也尝试过:

.configure(ConfigFactory.parseFile(new File("conf/application.conf")).resolve())

但是它导致Config,ApplicationLifecycle,Environment,Configuration注入崩溃。如何从application.conf获取注入设置到应用程序?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。文件conf/routes中似乎有一些错误,该错误阻止了应用程序并测试了执行。在向函数调用添加参数后,如下所示:

PUT /script controllers.ScriptController.uploadScriptPOST

收件人

PUT /script controllers.ScriptController.uploadScriptPOST(request: Request)

对于功能

public CompletionStage<Result> uploadScriptPOST(Http.Request request)

一切开始起作用。只是不考虑运行应用程序,而不是JUnit测试。