我正在尝试为测试配置新的属性,所以我创建了一个测试配置类:
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:无法打开Servlet上下文资源[/test.properties]
似乎找不到请求的属性。我建议您这样做:这能解决你的问题吗:
@PropertySource("classpath:test.properties")