如何测试我的个人资料?
这是我的考试
@Test
public void testDevProfile() throws Exception {
System.setProperty("spring.profiles.active", "dev");
Application.main(new String[0]);
String output = this.outputCapture.toString();
Assert.assertTrue(output.contains("The following profiles are active: dev"));
}
@Test
public void testUatProfile() throws Exception {
System.setProperty("spring.profiles.active", "uat");
Application.main(new String[0]);
String output = this.outputCapture.toString();
Assert.assertTrue(output.contains("The following profiles are active: uat"));
}
@Test
public void testPrdProfile() throws Exception {
System.setProperty("spring.profiles.active", "prd");
Application.main(new String[0]);
String output = this.outputCapture.toString();
Assert.assertFalse(output.contains("The following profiles are active: uat"));
Assert.assertFalse(output.contains("The following profiles are active: dev"));
Assert.assertFalse(output.contains("The following profiles are active: default"));
}
我的第一次测试执行正常,但其他测试失败。
org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [org.springframework.boot.actuate.endpoint.jmx.DataEndpointMBean@6cbe68e9] with key 'requestMappingEndpoint'; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Endpoint,name=requestMappingEndpoint
如何在下次测试开始前停止实例?或者哪种方法更好?
感谢
答案 0 :(得分:5)
我会这样做:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootApp.class)
@ActiveProfiles("dev")
public class ProfileDevTest {
@Value("${someProperty}")
private String someProperty;
@Test
public void testProperty() {
assertEquals("dev-value", someProperty);
}
}
上面的代码假设你有application-dev.properties
这样:
someProperty=dev-value
我希望测试的每个配置文件都有一个测试类,上面的这个测试用于配置文件 dev 。如果必须对活动配置文件(而不是属性)进行测试,则可以执行以下操作:
@Autowired
private Environment environment;
@Test
public void testActiveProfiles() {
assertArrayEquals(new String[]{"dev"}, environment.getActiveProfiles());
}