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

时间:2015-03-16 22:02:52

标签: intellij-idea dropwizard

对于dropwizard来说非常新,我正在尝试创建一个集成测试"确切地说"他们如何以此为例:

https://dropwizard.github.io/dropwizard/manual/testing.html
public class LoginAcceptanceTest {

@ClassRule
public static final DropwizardAppRule<TestConfiguration> RULE =
        new DropwizardAppRule<TestConfiguration>(MyApp.class, ResourceHelpers.resourceFilePath("dev-config.yml"));

@Test
public void loginHandlerRedirectsAfterPost() {
    //Client client = new JerseyClientBuilder(RULE.getEnvironment()).build("test client");


}
}

到目前为止有两个问题:首先,当我从intellij运行测试时,它失败并出现以下异常:

Caused by: java.lang.IllegalArgumentException: resource dev-config.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:140)
at java.lang.reflect.Field.acquireFieldAccessor(Field.java:1057)
at java.lang.reflect.Field.getFieldAccessor(Field.java:1038)
at java.lang.reflect.Field.get(Field.java:379)
at org.junit.runners.model.FrameworkField.get(FrameworkField.java:73)
at org.junit.runners.model.TestClass.getAnnotatedFieldValues(TestClass.java:230)
at org.junit.runners.ParentRunner.classRules(ParentRunner.java:255)
at org.junit.runners.ParentRunner.withClassRules(ParentRunner.java:244)
at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:194)
at org.junit.runners.ParentRunner.run(ParentRunner.java:362)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: resource dev-config.yml not found.
at io.dropwizard.testing.ResourceHelpers.resourceFilePath(ResourceHelpers.java:23)
at com.amplify.ckla.resources.TeacherConfigResourceIntegrationTest.<clinit>(TeacherConfigResourceIntegrationTest.java:12)
... 21 more
Caused by: java.lang.IllegalArgumentException: resource dev-config.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)
... 22 more

dev-config.yml绝对存在:

ls -a 
.               .idea               build.sh            pom.xml
..              CKLA.iml            dev-config.yml          src
.git                README.md                       target
.gitignore          develop.sh           

1 个答案:

答案 0 :(得分:3)

将程序的运行目录更改为项目的根目录(具有ls -a命令输出的目录)。目前很可能是从./target目录运行它。

或者如果您希望/需要从.target运行,请更改命令行选项以将配置文件引用到:../dev-config.yml

修改 正如@nightograph评论的那样,在 IntelliJ 中要做的就是右键单击项目文件夹并将目录标记为"Resources Root"