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

标签 configuration dropwizard integration-testing

我正在尝试使用以下应用程序规则组装 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/

    相关文章:

    php - SQLSTATE[42S02] : Base table or view not found: 1146 Table 'sf_sandbox.phpcr_workspaces' doesn't exist

    java - 自定义 Dropwizard 的/ping

    java - 如何使 dropwizard 应用程序忽略所有调用,直到启动完成

    java - 反序列化 Jersey 1 中的 List<Map<String, String>> QueryParam

    c# - 方法调用的所有引用的集成测试

    search - Solr 不搜索整数?

    Apache Config - 排除子目录

    ruby-on-rails - 需要循环遍历rspec中的一个数组,测试不运行

    Eclipse-Groovy 插件 : Adding new Groovy compiler versions

    java - 如何使用 Spring MVC Controller 作为集成测试的假端点?