英文:
Failed to parse configuration class, nested exception is java.io.FileNotFoundException: Could not open resource [/test.properties]
问题
我正在尝试为测试配置新属性,因此我创建了一个测试配置类:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
@ComponentScan("ar.com.yo")
@PropertySource("test.properties")
public class TestConfig {
}
属性文件位于 src/test/resources/test.properties
在测试类中:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class InsumoServiceTest {
...
}
当我执行测试时,出现以下错误:
无法解析配置类 [ar.com.yo.myproject.main.TestConfig];嵌套异常为 java.io.FileNotFoundException: 无法打开 ServletContext 资源 [/test.properties]
英文:
I´m trying to configurate new properties for the test, so I created a test config class:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
@ComponentScan("ar.com.yo")
@PropertySource("test.properties")
public class TestConfig {
}
Properties file is in src/test/resources/test.properties
and in the test class :
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class InsumoServiceTest {
...
}
when I execute the tests the error is:
Failed to parse configuration class [ar.com.yo.myproject.main.TestConfig]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/test.properties]
答案1
得分: 1
似乎找不到所请求的属性。我建议这样做:
解决你的问题的方法是:
@PropertySource("classpath:test.properties")
英文:
It seems that the requested properties can't be found. I would recommend doing this:
is that resolve your problem :
@PropertySource("classpath:test.properties")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论