Dropwizard 集成测试:找不到配置资源文件
Dropwizard integration test: config resource file not found
我正在尝试 assemble 使用以下应用程序规则进行 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
根据 ,我应该只使用父文件夹 ../config_for_test.yml
。但是,这并不能解决问题。
好的,我刚刚自己找到了解决方案。因此 Dropwizard 将在 ${basedir}/src/test/resources
中查找配置文件,因为这是 Maven 查找任何文件的默认目录。另一方面,当应用程序是 运行 时,默认目录只是 ${basedir}
。因此,以下方法将有所帮助:
- 只需使用
../../../config_for_test.yml
进行集成测试。
- 将配置文件移动到
${basedir}/src/test/resources
目录。
我使用方法 2 并进一步将相应的 config_for_release.yml
移动到 src/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 的 Package Explorer 的相应文件夹中,我不会不小心打开错误的配置文件。
我正在尝试 assemble 使用以下应用程序规则进行 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
根据 ../config_for_test.yml
。但是,这并不能解决问题。
好的,我刚刚自己找到了解决方案。因此 Dropwizard 将在 ${basedir}/src/test/resources
中查找配置文件,因为这是 Maven 查找任何文件的默认目录。另一方面,当应用程序是 运行 时,默认目录只是 ${basedir}
。因此,以下方法将有所帮助:
- 只需使用
../../../config_for_test.yml
进行集成测试。 - 将配置文件移动到
${basedir}/src/test/resources
目录。
我使用方法 2 并进一步将相应的 config_for_release.yml
移动到 src/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 的 Package Explorer 的相应文件夹中,我不会不小心打开错误的配置文件。