我正在尝试使用以下应用规则组装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
。但是,这并没有解决问题。
答案 0 :(得分:4)
好的,我刚刚找到了自己的解决方案。所以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的相应文件夹中,我不小心打开了错误的配置文件。