Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard integration test: config resource file not found

I am trying to assemble a Dropwizard integration test with the following app rule:

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

When I run the test I get the following error:

java.lang.IllegalArgumentException: resource config_for_test.yml not found.

The full stack trace is:

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

According to another stackoverflow entry I should just use the parent folder ../config_for_test.yml. However, this does not solve the problem.

like image 680
Bastian Avatar asked Apr 15 '15 10:04

Bastian


1 Answers

Ok, I just found a solution on my own. So Dropwizard will look in ${basedir}/src/test/resources for the configuration file as this is maven's default directory to look for any files. On the other hand, when the application is run the default directory is just ${basedir}. Hence, the following approaches will help:

  1. Just use ../../../config_for_test.yml for the integration test.
  2. Move the config file to the ${basedir}/src/test/resources directory.

I used approach 2 and furthermore moved the according config_for_release.yml to src/main/resources in order to have a symmetric file structure. This, however, leaves the basedir directory empty when the program is run in normal mode (with the arguments server config_for_release.yml). Thus, one can adapt the argument to server src/main/resources/config_for_release.yml. Since I do not like to use such long paths on method startup I chose a different solution: I use the maven copy plugin to copy the file to the target folder:

<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>   

The program is then started with server target/config_for_release.yml. I use the target folder mainly so that the config file is hidden inside the according folder of Eclipse's Package Explorer and I do not accidentally open the wrong configuration file.

like image 110
Bastian Avatar answered Sep 29 '22 13:09

Bastian