简体   繁体   English

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

[英]Play Framework 2.7.0 Config for tests without general configurations from application.conf

In Play's application.conf I have some configs for my app. 在Play的application.conf我有一些针对我的应用程序的配置。 For example: 例如:

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

During Play migration from 2.6.20 to 2.7.0, I discovered that my tests don't work because pew.server-script isn't in the Config. 在Play从2.6.20迁移到2.7.0的过程中,我发现我的测试无法正常进行,因为pew.server-script不在Config中。 This is an example test: 这是一个示例测试:

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());
    });
}

It fails on initialization of Module when calling GuiceApplicationBuilder.build(): 调用GuiceApplicationBuilder.build()时,在初始化模块时失败:

bind(ConfigService.class).asEagerSingleton();

ConfigService looks like this: 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
}

With this exception: 例外情况:

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 

Also Akka crashes while router is creating with settings from config and JPA stuff - necessary settings not included in Config. 同样,当路由器使用config和JPA内容中的设置创建路由器时,Akka也会崩溃-Config中未包含必要的设置。 I have checked with IntelliJ IDEA debug - from "pew" in Config instance there is only pew.scriptFolder = "example", which has been set up in additional config ( prepareApplication() function). 我已经使用IntelliJ IDEA调试进行了检查-从Config实例中的“ pew”开始,只有pew.scriptFolder =“ example”,它已在其他config中设置( prepareApplication()函数)。
I also tried this: 我也试过这个:

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

But it leads to the crash of Config, ApplicationLifecycle, Environment, Configuration injection. 但这会导致Config,ApplicationLifecycle,环境,Configuration注入崩溃。 How to get inject settings from application.conf to Application? 如何从application.conf获取注入设置到Application?

I found out a solution. 我找到了解决方案。 It looks like in file conf/routes there were some errors, which blocked the application and tests execution. 似乎在文件conf/routes存在一些错误,这些错误阻止了应用程序并测试了执行。 After adding parameters to a function call like this: 在向函数调用中添加参数后,如下所示:

PUT /script controllers.ScriptController.uploadScriptPOST

To

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

For a function 对于功能

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

Everything began to work. 一切开始起作用。 Just didn't think to run the application, not JUnit tests. 只是不考虑运行应用程序,而不是JUnit测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM