我试图将一个test.properties
文件传递给@TestPropertySource
注释,如下所示:
@SpringBootTest
@TestPropertySource(locations = "classpath:test.properties")
public class ApplicationTests {
@Test
public void contextLoads() {
}
@Test
public void main() {
Application.main(new String[] {});
}
}
然后我调用主应用程序,希望使用test.properties
。Application.main
是一个标准的引导应用程序:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
也许我误解了@TestPropertySource
的目的
谢谢你的帮助
发布于 2022-07-04 12:30:40
下面是用文档编写的
@TestPropertySource是一个类级注释,用于配置属性文件和内联属性()的位置(),以便添加到环境的PropertySources集合中,用于集成测试的ApplicationContext。
因此,基本上,可以使用测试属性源来选择性地覆盖在系统和应用程序属性源中定义的属性。
如果将@TestPropertySource
声明为空注释(即,对locations()或properties ()没有显式值),则将尝试检测相对于声明注释的类的默认属性文件。例如,如果带注释的测试类是com.example.MyTest
,则相应的默认属性文件是"classpath:com/example/MyTest.properties
“。如果无法检测到默认值,则将引发IllegalStateException
。
在您的示例中,文件test.properties
应该放在resources
文件夹中。
您可以检查这个示例,看看它是如何工作的。
https://stackoverflow.com/questions/72856265
复制相似问题