Dropwizard集成测试:找不到配置资源文件

Bas*_*ian 6 configuration integration-testing dropwizard

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

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

运行测试时,出现以下错误:

java.lang.IllegalArgumentException: resource config_for_test.yml not found.
Run Code Online (Sandbox Code Playgroud)

完整的堆栈跟踪为:

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
Run Code Online (Sandbox Code Playgroud)

根据另一个stackoverflow条目,我应该只使用父文件夹../config_for_test.yml。但是,这不能解决问题。

Bas*_*ian 5

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

  1. ../../../config_for_test.yml用于集成测试。
  2. 将配置文件移动到${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复制插件将文件复制到目标文件夹:

<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>   
Run Code Online (Sandbox Code Playgroud)

该程序然后从开始server target/config_for_release.yml。我主要使用目标文件夹,以便将配置文件隐藏在Eclipse的Package Explorer的相应文件夹内,并且我不会意外打开错误的配置文件。