Har*_*ash 3 java junit spring integration-testing spring-boot
我试图将application.properties配置文件用于使用JUnit的集成测试,以便检查两个不同的平台。
我尝试使用application.properties包含两个平台的通用配置的基本配置文件来执行此操作,此外,我还添加了属性文件application-tensorflow.properties application-caffe.properties为每个平台具有特定平台配置的,但是我发现它在JUnit中的工作方式与我以前在主应用程序中使用的方法。
我的测试配置类如下所示:
@Configuration
@PropertySource("classpath:application.properties")
@CompileStatic
@EnableConfigurationProperties
class TestConfig {...}
Run Code Online (Sandbox Code Playgroud)
我正在使用,@PropertySource("classpath:application.properties")因此它将识别我的基本配置,我也在那里写了一篇文章spring.profiles.active=tensorflow,希望它可以识别tensorflow应用程序配置文件,但是它不会从file:中读取/src/test/resources/application-tensorflow.properties,也不会/src/main/resources/application-tensorflow.properties像在主应用程序中那样从中读取。
有没有一种特殊的方法可以在JUnit测试中指定弹簧轮廓?实现我正在尝试的最佳实践是什么?
首先:添加@ActiveProfiles到测试类中以定义活动配置文件。
另外,您需要配置应加载配置文件。有两种选择:
@ContextConfiguration(classes = TheConfiguration.class, initializers = ConfigFileApplicationContextInitializer.class)@SpringBootTest示例测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({ "test" })
public class DummyTest {
@Autowired
private Environment env;
@Test
public void readProps() {
String value = env.getProperty("prop1") + " " + env.getProperty("prop2");
assertEquals("Hello World", value);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,文件src/test/resources/application.properties和src/test/resources/application-test.properties被评估。
| 归档时间: |
|
| 查看次数: |
2880 次 |
| 最近记录: |