Play Framework 2.7.0 Config用于没有application.conf的常规配置的测试

问题描述 投票:0回答:1

在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 

此外,当路由器使用配置和JPA设置创建时,Akka崩溃 - Config中未包含必要的设置。我已经使用IntelliJ IDEA调试 - 从Config实例中的“pew”只有pew.scriptFolder =“example”,它已在附加配置(prepareApplication()函数)中设置。 我也试过这个:

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

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

java testing playframework guice play-framework-2.7
1个回答
0
投票

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

PUT /script controllers.ScriptController.uploadScriptPOST

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

对于一个功能

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

一切都开始奏效了。只是没想到运行应​​用程序,而不是JUnit测试。

© www.soinside.com 2019 - 2024. All rights reserved.