gpt4 book ai didi

configuration - Dropwizard 集成测试 : config resource file not found

转载 作者:行者123 更新时间:2023-12-04 15:10:23 25 4
gpt4 key购买 nike

我正在尝试使用以下应用程序规则组装 Dropwizard 集成测试:

public static final DropwizardAppRule<MyConfiguration> RULE = new DropwizardAppRule<MyConfiguration>(
MyApplication.class, ResourceHelpers.resourceFilePath("config_for_test.yml"));

当我运行测试时,我收到以下错误:
java.lang.IllegalArgumentException: resource config_for_test.yml not found.

完整的堆栈跟踪是:
java.lang.ExceptionInInitializerError
at sun.misc.Unsafe.ensureClassInitialized(Native Method)
at sun.reflect.UnsafeFieldAccessorFactory.newFieldAccessor(UnsafeFieldAccessorFactory.java:43)
at sun.reflect.ReflectionFactory.newFieldAccessor(ReflectionFactory.java:142)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1082)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1063)
at java.lang.reflect.Field.get(Field.java:387)
at org.junit.runners.model.FrameworkField.get(FrameworkField.java:69)
at org.junit.runners.model.TestClass.getAnnotatedFieldValues(TestClass.java:156)
at org.junit.runners.ParentRunner.classRules(ParentRunner.java:215)
at org.junit.runners.ParentRunner.withClassRules(ParentRunner.java:203)
at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:163)
at org.junit.runners.ParentRunner.run(ParentRunner.java:308)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: resource config_for_test.yml not found.
at io.dropwizard.testing.ResourceHelpers.resourceFilePath(ResourceHelpers.java:23)
at de.emundo.sortimo.resource.TaskAdditionTest.<clinit>(TaskAdditionTest.java:28)
... 18 more
Caused by: java.lang.IllegalArgumentException: resource ../config_for_test.yml not found.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:145)
at com.google.common.io.Resources.getResource(Resources.java:197)
at io.dropwizard.testing.ResourceHelpers.resourceFilePath(ResourceHelpers.java:21)
... 19 more

根据 another stackoverflow entry我应该只使用父文件夹 ../config_for_test.yml .但是,这并不能解决问题。

最佳答案

好的,我只是自己找到了解决方案。所以 Dropwizard 会查看 ${basedir}/src/test/resources对于配置文件,因为这是 maven 查找任何文件的默认目录。另一方面,当应用程序运行时,默认目录只是 ${basedir} .因此,以下方法将有所帮助:

  • 只需使用 ../../../config_for_test.yml用于集成测试。
  • 将配置文件移动到 ${basedir}/src/test/resources目录。

  • 我使用了方法 2 并进一步移动了根据 config_for_release.ymlsrc/main/resources为了有一个对称的文件结构。但是,当程序在正常模式下运行时(使用参数 server config_for_release.yml ),这会使 basedir 目录为空。因此,可以将参数调整为 server src/main/resources/config_for_release.yml .由于我不喜欢在方法启动时使用这么长的路径,我选择了一个不同的解决方案:我使用 maven copy plugin将文件复制到目标文件夹:

    <plugin>                                                            
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <executions>
    <execution>
    <id>copy-resources</id>
    <!-- here the phase you need -->
    <phase>validate</phase>
    <goals>
    <goal>copy-resources</goal>
    </goals>
    <configuration>
    <outputDirectory>${basedir}/target</outputDirectory>
    <resources>
    <resource>
    Å <directory>src/main/resources</directory>
    <filtering>true</filtering>
    <includes>
    <include>**/*.yml</include>
    </includes>
    </resource>
    </resources>
    </configuration>
    </execution>
    </executions>
    </plugin>

    然后程序以 server target/config_for_release.yml 启动.我主要使用目标文件夹,以便配置文件隐藏在 Eclipse 包资源管理器的相应文件夹中,并且我不会意外打开错误的配置文件。

    关于configuration - Dropwizard 集成测试 : config resource file not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647284/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com